home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / v cisle / htttrack / httrack-3.41-3.exe / {app} / src / htsparse.c < prev    next >
C/C++ Source or Header  |  2006-11-12  |  201KB  |  4,561 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: htsparse.c parser                                      */
  34. /*       html/javascript/css parser                             */
  35. /*       and other parser routines                              */
  36. /* Author: Xavier Roche                                         */
  37. /* ------------------------------------------------------------ */
  38.  
  39.  
  40. /* Internal engine bytecode */
  41. #define HTS_INTERNAL_BYTECODE
  42.  
  43. #ifndef  _WIN32_WCE
  44. #include <fcntl.h>
  45. #endif
  46. #include <ctype.h>
  47.  
  48. /* File defs */
  49. #include "htscore.h"
  50.  
  51. /* specific definitions */
  52. #include "htsbase.h"
  53. #include "htsnet.h"
  54. #include "htsbauth.h"
  55. #include "htsmd5.h"
  56. #include "htsindex.h"
  57.  
  58. /* external modules */
  59. #include "htsmodules.h"
  60.  
  61. // htswrap_add
  62. #include "htswrap.h"
  63.  
  64. // parser
  65. #include "htsparse.h"
  66. #include "htsback.h"
  67.  
  68. // specific defines
  69. #define urladr   (liens[ptr]->adr)
  70. #define urlfil   (liens[ptr]->fil)
  71. #define savename (liens[ptr]->sav)
  72. #define parenturladr   (liens[liens[ptr]->precedent]->adr)
  73. #define parenturlfil   (liens[liens[ptr]->precedent]->fil)
  74. #define parentsavename (liens[liens[ptr]->precedent]->sav)
  75. #define relativeurladr   ((!parent_relative)?urladr:parenturladr)
  76. #define relativeurlfil   ((!parent_relative)?urlfil:parenturlfil)
  77. #define relativesavename ((!parent_relative)?savename:parentsavename)
  78.  
  79. #define test_flush if (opt->flush) { if (opt->log) { fflush(opt->log); } if (opt->log) { fflush(opt->log);  } }
  80.  
  81. // does nothing
  82. #define XH_uninit do {} while(0)
  83.  
  84. // version optimisΘe, qui permet de ne pas toucher aux html non modifiΘs (update)
  85. #define REALLOC_SIZE 8192
  86. #define HT_ADD_CHK(A) if (((int) (A)+ht_len+1) >= ht_size) { \
  87.   ht_size=(A)+ht_len+REALLOC_SIZE; \
  88.   ht_buff=(char*) realloct(ht_buff,ht_size); \
  89.   if (ht_buff==NULL) { \
  90.   printf("PANIC! : Not enough memory [%d]\n",__LINE__); \
  91.   XH_uninit; \
  92.   abortLogFmt("not enough memory for current html document in HT_ADD_CHK : realloct(%d) failed" _ ht_size); \
  93.   exit(1); \
  94.   } \
  95. } \
  96.   ht_len+=A;
  97. #define HT_ADD_ADR \
  98.   if ((opt->getmode & 1) && (ptr>0)) { \
  99.   size_t i = ((size_t) (adr - lastsaved)),j=ht_len; HT_ADD_CHK(i) \
  100.   memcpy(ht_buff+j, lastsaved, i); \
  101.   ht_buff[j+i]='\0'; \
  102.   lastsaved=adr; \
  103.   }
  104. #define HT_ADD(A) \
  105.   if ((opt->getmode & 1) && (ptr>0)) { \
  106.   size_t i_ = strlen(A), j_ = ht_len; \
  107.   if (i_) { \
  108.   HT_ADD_CHK(i_) \
  109.   memcpy(ht_buff+j_, A, i_); \
  110.   ht_buff[j_+i_]='\0'; \
  111.   } }
  112. #define HT_ADD_HTMLESCAPED(A) \
  113.   if ((opt->getmode & 1) && (ptr>0)) { \
  114.     size_t i_, j_; \
  115.     char BIGSTK tempo_[HTS_URLMAXSIZE*2]; \
  116.     escape_for_html_print(A, tempo_); \
  117.     i_=strlen(tempo_); \
  118.     j_=ht_len; \
  119.     if (i_) { \
  120.     HT_ADD_CHK(i_) \
  121.     memcpy(ht_buff+j_, tempo_, i_); \
  122.     ht_buff[j_+i_]='\0'; \
  123.   } }
  124. #define HT_ADD_HTMLESCAPED_FULL(A) \
  125.   if ((opt->getmode & 1) && (ptr>0)) { \
  126.     size_t i_, j_; \
  127.     char BIGSTK tempo_[HTS_URLMAXSIZE*2]; \
  128.     escape_for_html_print_full(A, tempo_); \
  129.     i_=strlen(tempo_); \
  130.     j_=ht_len; \
  131.     if (i_) { \
  132.     HT_ADD_CHK(i_) \
  133.     memcpy(ht_buff+j_, tempo_, i_); \
  134.     ht_buff[j_+i_]='\0'; \
  135.   } }
  136. #define HT_ADD_START \
  137.   size_t ht_size=(size_t)(r->size*5)/4+REALLOC_SIZE; \
  138.   size_t ht_len=0; \
  139.   char* ht_buff=NULL; \
  140.   if ((opt->getmode & 1) && (ptr>0)) { \
  141.   ht_buff=(char*) malloct(ht_size); \
  142.   if (ht_buff==NULL) { \
  143.   printf("PANIC! : Not enough memory [%d]\n",__LINE__); \
  144.   XH_uninit; \
  145.   abortLogFmt("not enough memory for current html document in HT_ADD_START : malloct(%d) failed" _ (int) ht_size); \
  146.   exit(1); \
  147.   } \
  148.   ht_buff[0]='\0'; \
  149.   }
  150. #define HT_ADD_END { \
  151.   int ok=0;\
  152.   if (ht_buff) { \
  153.     char digest[32+2];\
  154.     off_t fsize_old = fsize(fconv(OPT_GET_BUFF(opt),savename));\
  155.     digest[0]='\0';\
  156.     domd5mem(ht_buff,ht_len,digest,1);\
  157.     if (fsize_old==ht_len) { \
  158.       int mlen = 0;\
  159.       char* mbuff;\
  160.       cache_readdata(cache,"//[HTML-MD5]//",savename,&mbuff,&mlen);\
  161.       if (mlen) \
  162.         mbuff[mlen]='\0';\
  163.       if ((mlen == 32) && (strcmp(((mbuff!=NULL)?mbuff:""),digest)==0)) {\
  164.         ok=1;\
  165.         if ( (opt->debug>1) && (opt->log!=NULL) ) {\
  166.           HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File not re-written (md5): %s"LF,savename);\
  167.           test_flush;\
  168.         }\
  169.       } else {\
  170.         ok=0;\
  171.       } \
  172.     }\
  173.     if (!ok) { \
  174.       file_notify(opt,urladr, urlfil, savename, 1, 1, r->notmodified); \
  175.       fp=filecreate(&opt->state.strc, savename); \
  176.       if (fp) { \
  177.         if (ht_len>0) {\
  178.         if (fwrite(ht_buff,1,ht_len,fp) != ht_len) { \
  179.           int fcheck;\
  180.           if ((fcheck=check_fatal_io_errno())) {\
  181.             opt->state.exit_xh=-1;\
  182.           }\
  183.           if (opt->log) {   \
  184.             int last_errno = errno; \
  185.             HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Unable to write HTML file %s: %s"LF, savename, strerror(last_errno));\
  186.             if (fcheck) {\
  187.               HTS_LOG(opt,LOG_ERROR);\
  188.               fprintf(opt->log,"* * Fatal write error, giving up"LF);\
  189.             }\
  190.             test_flush;\
  191.           }\
  192.         }\
  193.         }\
  194.         fclose(fp); fp=NULL; \
  195.         if (strnotempty(r->lastmodified)) \
  196.         set_filetime_rfc822(savename,r->lastmodified); \
  197.       } else {\
  198.         int fcheck;\
  199.         if ((fcheck=check_fatal_io_errno())) {\
  200.                   HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Mirror aborted: disk full or filesystem problems"LF); \
  201.                     test_flush; \
  202.           opt->state.exit_xh=-1;\
  203.         }\
  204.         if (opt->log) { \
  205.           int last_errno = errno; \
  206.           HTS_LOG(opt,LOG_ERROR);\
  207.           fprintf(opt->log,"Unable to save file %s : %s"LF, savename, strerror(last_errno));\
  208.           if (fcheck) {\
  209.             HTS_LOG(opt,LOG_ERROR);\
  210.             fprintf(opt->log,"* * Fatal write error, giving up"LF);\
  211.           }\
  212.           test_flush;\
  213.         }\
  214.       }\
  215.     } else {\
  216.       file_notify(opt,urladr, urlfil, savename, 0, 0, r->notmodified); \
  217.       filenote(&opt->state.strc, savename,NULL); \
  218.     }\
  219.     if (cache->ndx)\
  220.       cache_writedata(cache->ndx,cache->dat,"//[HTML-MD5]//",savename,digest,(int)strlen(digest));\
  221.   } \
  222.   freet(ht_buff); ht_buff=NULL; \
  223. }
  224. #define HT_ADD_FOP 
  225.  
  226. // COPY IN HTSCORE.C
  227. #define HT_INDEX_END do { \
  228.   if (!makeindex_done) { \
  229.   if (makeindex_fp) { \
  230.   char BIGSTK tempo[1024]; \
  231.   if (makeindex_links == 1) { \
  232.   sprintf(tempo,"<meta HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=%s\">"CRLF,makeindex_firstlink); \
  233.   } else \
  234.   tempo[0]='\0'; \
  235.   fprintf(makeindex_fp,template_footer, \
  236.   "<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->", \
  237.   tempo \
  238.   ); \
  239.   fflush(makeindex_fp); \
  240.   fclose(makeindex_fp);  /* α ne pas oublier sinon on passe une nuit blanche */  \
  241.   makeindex_fp=NULL; \
  242.   usercommand(opt,0,NULL,fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_html),"index.html"),"primary","primary");  \
  243.   } \
  244.   } \
  245.   makeindex_done=1;    /* ok c'est fait */  \
  246. } while(0)
  247.  
  248. // Enregistrement d'un lien:
  249. // on calcule la taille nΘcessaire: taille des 3 chaεnes α stocker (taille forcΘe paire, plus 2 octets de sΘcuritΘ)
  250. // puis on vΘrifie qu'on a assez de marge dans le buffer - sinon on en rΘalloue un autre
  251. // enfin on Θcrit α l'adresse courante du buffer, qu'on incrΘmente. on dΘcrΘmente la taille dispo d'autant ensuite
  252. // codebase: si non nul et si .class stockee on le note pour chemin primaire pour classes
  253. // FA,FS: former_adr et former_fil, lien original
  254. #define liens_record_sav_len(A) 
  255.  
  256. // COPIE DE HTSCORE.C
  257.  
  258. #define liens_record(A,F,S,FA,FF) { \
  259.   int notecode=0; \
  260.   size_t lienurl_len=((sizeof(lien_url)+HTS_ALIGN-1)/HTS_ALIGN)*HTS_ALIGN,\
  261.   adr_len=strlen(A),\
  262.   fil_len=strlen(F),\
  263.   sav_len=strlen(S),\
  264.   cod_len=0,\
  265.   former_adr_len=strlen(FA),\
  266.   former_fil_len=strlen(FF); \
  267.   if (former_adr_len>0) {\
  268.     former_adr_len=(former_adr_len/HTS_ALIGN)*HTS_ALIGN+HTS_ALIGN*2; \
  269.     former_fil_len=(former_fil_len/HTS_ALIGN)*HTS_ALIGN+HTS_ALIGN*2; \
  270.   } else \
  271.     former_adr_len=former_fil_len=0;\
  272.   if (strlen(F)>6) if (strnotempty(codebase)) if (strfield(F+strlen(F)-6,".class")) {\
  273.     notecode=1; \
  274.     cod_len=strlen(codebase); \
  275.     cod_len=(cod_len/HTS_ALIGN)*HTS_ALIGN+HTS_ALIGN*2; \
  276.   } \
  277.   adr_len=(adr_len/HTS_ALIGN)*HTS_ALIGN+HTS_ALIGN*2; \
  278.   fil_len=(fil_len/HTS_ALIGN)*HTS_ALIGN+HTS_ALIGN*2; \
  279.   sav_len=(sav_len/HTS_ALIGN)*HTS_ALIGN+HTS_ALIGN*2; \
  280.   if ((int) lien_size < (int) (adr_len+fil_len+sav_len+cod_len+former_adr_len+former_fil_len+lienurl_len)) { \
  281.     lien_buffer=(char*) ((void*) calloct(add_tab_alloc,1)); \
  282.     lien_size=add_tab_alloc; \
  283.     if (lien_buffer!=NULL) { \
  284.     liens[lien_tot]=(lien_url*) (void*) lien_buffer; lien_buffer+=lienurl_len; lien_size-=lienurl_len; \
  285.     liens[lien_tot]->firstblock=1; \
  286.     } \
  287.   } else { \
  288.     liens[lien_tot]=(lien_url*) (void*) lien_buffer; lien_buffer+=lienurl_len; lien_size-=lienurl_len; \
  289.     liens[lien_tot]->firstblock=0; \
  290.   } \
  291.   if (liens[lien_tot]!=NULL) { \
  292.     liens[lien_tot]->adr=lien_buffer; lien_buffer+=adr_len; lien_size-=adr_len; \
  293.     liens[lien_tot]->fil=lien_buffer; lien_buffer+=fil_len; lien_size-=fil_len; \
  294.     liens[lien_tot]->sav=lien_buffer; lien_buffer+=sav_len; lien_size-=sav_len; \
  295.     liens[lien_tot]->cod=NULL; \
  296.     if (notecode) { \
  297.       liens[lien_tot]->cod=lien_buffer; \
  298.       lien_buffer+=cod_len; \
  299.       lien_size-=cod_len; \
  300.       strcpybuff(liens[lien_tot]->cod,codebase); \
  301.     } \
  302.     if (former_adr_len>0) {\
  303.       liens[lien_tot]->former_adr=lien_buffer; lien_buffer+=former_adr_len; lien_size-=former_adr_len; \
  304.       liens[lien_tot]->former_fil=lien_buffer; lien_buffer+=former_fil_len; lien_size-=former_fil_len; \
  305.       strcpybuff(liens[lien_tot]->former_adr,FA); \
  306.       strcpybuff(liens[lien_tot]->former_fil,FF); \
  307.     }\
  308.     strcpybuff(liens[lien_tot]->adr,A); \
  309.     strcpybuff(liens[lien_tot]->fil,F); \
  310.     strcpybuff(liens[lien_tot]->sav,S); \
  311.     liens_record_sav_len(liens[lien_tot]); \
  312.     hash_write(hashptr,lien_tot,opt->urlhack);  \
  313.   } \
  314. }
  315.  
  316. #define ENGINE_LOAD_CONTEXT() \
  317.   ENGINE_LOAD_CONTEXT_BASE(); \
  318.   /* */ \
  319.   htsblk* r HTS_UNUSED = stre->r_; \
  320.   hash_struct* hash HTS_UNUSED = stre->hash_; \
  321.   int lien_max HTS_UNUSED = *stre->lien_max_; \
  322.   /* */ \
  323.   int error HTS_UNUSED = * stre->error_; \
  324.   int store_errpage HTS_UNUSED = * stre->store_errpage_; \
  325.   char* codebase HTS_UNUSED = stre->codebase; \
  326.   char* base HTS_UNUSED = stre->base; \
  327.   /* */ \
  328.   int makeindex_done HTS_UNUSED = *stre->makeindex_done_; \
  329.   FILE* makeindex_fp HTS_UNUSED = *stre->makeindex_fp_; \
  330.   int makeindex_links HTS_UNUSED = *stre->makeindex_links_; \
  331.   char* makeindex_firstlink HTS_UNUSED = stre->makeindex_firstlink_; \
  332.   /* */ \
  333.   char *template_header HTS_UNUSED = stre->template_header_; \
  334.   char *template_body HTS_UNUSED = stre->template_body_; \
  335.   char *template_footer HTS_UNUSED = stre->template_footer_; \
  336.   /* */ \
  337.   LLint stat_fragment HTS_UNUSED = *stre->stat_fragment_; \
  338.   TStamp makestat_time HTS_UNUSED = stre->makestat_time; \
  339.   FILE* makestat_fp HTS_UNUSED = stre->makestat_fp
  340.  
  341. #define ENGINE_SAVE_CONTEXT() \
  342.   ENGINE_SAVE_CONTEXT_BASE(); \
  343.   /* */ \
  344.   * stre->error_ = error; \
  345.   * stre->store_errpage_ = store_errpage; \
  346.   * stre->lien_max_ = lien_max; \
  347.   /* */ \
  348.   *stre->makeindex_done_ = makeindex_done; \
  349.   *stre->makeindex_fp_ = makeindex_fp; \
  350.   *stre->makeindex_links_ = makeindex_links; \
  351.   /* */ \
  352.   *stre->stat_fragment_ = stat_fragment
  353.  
  354. #define _FILTERS     (*opt->filters.filters)
  355. #define _FILTERS_PTR (opt->filters.filptr)
  356. #define _ROBOTS      ((robots_wizard*)opt->robotsptr)
  357.  
  358. /* Apply current *adr character for the script automate */
  359. #define AUTOMATE_LOOKUP_CURRENT_ADR() do { \
  360.   if (inscript) { \
  361.   int new_state_pos; \
  362.   new_state_pos=inscript_state[inscript_state_pos][(unsigned char)*adr]; \
  363.   if (new_state_pos < 0) { \
  364.   new_state_pos=inscript_state[inscript_state_pos][INSCRIPT_DEFAULT]; \
  365.   } \
  366.   assertf(new_state_pos >= 0); \
  367.   assertf(new_state_pos*sizeof(inscript_state[0]) < sizeof(inscript_state)); \
  368.   inscript_state_pos=new_state_pos; \
  369.   } \
  370. } while(0)  
  371.  
  372. /* Increment current pointer to 'steps' characters, modifying automate if necessary */
  373. #define INCREMENT_CURRENT_ADR(steps) do { \
  374.   int steps__ = (int) ( steps ); \
  375.   while(steps__ > 0) { \
  376.   adr++; \
  377.   AUTOMATE_LOOKUP_CURRENT_ADR(); \
  378.   steps__ --; \
  379.   } \
  380. } while(0)
  381.  
  382.  
  383. /* Main parser */
  384. int htsparse(htsmoduleStruct* str, htsmoduleStructExtended* stre) {
  385.   /* Load engine variables */
  386.   ENGINE_LOAD_CONTEXT();
  387.     char catbuff[CATBUFF_SIZE];
  388.  
  389.   {
  390.     char* cAddr = r->adr;
  391.     int cSize = (int) r->size;
  392.     if ( (opt->debug>0) && (opt->log!=NULL) ) {
  393.       HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: preprocess-html: %s%s"LF, urladr, urlfil);
  394.     }
  395.     if (RUN_CALLBACK4(opt, preprocess, &cAddr, &cSize, urladr, urlfil) == 1) {
  396.       r->adr = cAddr;
  397.       r->size = cSize;
  398.     }
  399.   }
  400.   if (RUN_CALLBACK4(opt, check_html, r->adr,(int)r->size,urladr,urlfil)) {
  401.     FILE* fp=NULL;      // fichier Θcrit localement 
  402.     char* adr=r->adr;    // pointeur (on parcourt)
  403.     char* lastsaved;    // adresse du dernier octet sauvΘ + 1
  404.     if ( (opt->debug>1) && (opt->log!=NULL) ) {
  405.       HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"scanning file %s%s (%s).."LF, urladr, urlfil, savename); test_flush;
  406.     }
  407.  
  408.  
  409.     // Indexing!
  410. #if HTS_MAKE_KEYWORD_INDEX
  411.     if (opt->kindex) {
  412.       if (index_keyword(r->adr,r->size,r->contenttype,savename,StringBuff(opt->path_html))) {
  413.         if ( (opt->debug>1) && (opt->log!=NULL) ) {
  414.           HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"indexing file..done"LF); test_flush;
  415.         }
  416.       } else {
  417.         if ( (opt->debug>1) && (opt->log!=NULL) ) {
  418.           HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"indexing file..error!"LF); test_flush;
  419.         }
  420.       }
  421.     }
  422. #endif
  423.  
  424.     // Now, parsing
  425.     if ((opt->getmode & 1) && (ptr>0)) {  // rΘcupΘrer les html sur disque       
  426.       // crΘer le fichier html local
  427.       HT_ADD_FOP;   // Θcrire peu α peu le fichier
  428.     }
  429.  
  430.     if (!error) {
  431.       int detect_title=0;  // dΘtection  du title
  432.       int back_add_stats = opt->state.back_add_stats;
  433.       //
  434.       char* in_media=NULL; // in other media type (real media and so..)
  435.       int intag=0;         // on est dans un tag
  436.       int incomment=0;     // dans un <!--
  437.       int inscript=0;      // dans un scipt pour applets javascript)
  438.       signed char inscript_state[10][257];
  439.       typedef enum { 
  440.         INSCRIPT_START=0,
  441.         INSCRIPT_ANTISLASH,
  442.         INSCRIPT_INQUOTE,
  443.         INSCRIPT_INQUOTE2,
  444.         INSCRIPT_SLASH,
  445.         INSCRIPT_SLASHSLASH,
  446.         INSCRIPT_COMMENT,
  447.         INSCRIPT_COMMENT2,
  448.         INSCRIPT_ANTISLASH_IN_QUOTE,
  449.         INSCRIPT_ANTISLASH_IN_QUOTE2,
  450.         INSCRIPT_DEFAULT=256
  451.       } INSCRIPT;
  452.       INSCRIPT inscript_state_pos=INSCRIPT_START;
  453.       char* inscript_name=NULL; // script tag name
  454.       int inscript_tag=0;  // on est dans un <body onLoad="... terminΘ par >
  455.       char inscript_tag_lastc='\0';
  456.       // terminaison (" ou ') du "<body onLoad=.."
  457.       int inscriptgen=0;     // on est dans un code gΘnΘrant, ex aprΦs obj.write("..
  458.       //int inscript_check_comments=0, inscript_in_comments=0;    // javascript comments
  459.       char scriptgen_q='\0'; // caractΦre faisant office de guillemet (' ou ")
  460.       //int no_esc_utf=0;      // ne pas echapper chars > 127
  461.       int nofollow=0;        // ne pas scanner
  462.       //
  463.       int parseall_lastc='\0';     // dernier caractΦre parsΘ pour parseall
  464.       //int parseall_incomment=0;   // dans un /* */ (exemple: a = /* URL */ "img.gif";)
  465.       //
  466.       char* intag_start = adr;
  467.             char* intag_name = NULL;
  468.       char* intag_startattr=NULL;
  469.       int intag_start_valid=0;
  470.       int intag_ctype=0;
  471.       //
  472.       int   parent_relative=0;    // the parent is the base path (.js, .css..)
  473.       HT_ADD_START;    // dΘbuter
  474.       lastsaved=adr;
  475.  
  476.       /* Initialize script automate for comments, quotes.. */
  477.       memset(inscript_state, 0xff, sizeof(inscript_state));
  478.       inscript_state[INSCRIPT_START][INSCRIPT_DEFAULT]=INSCRIPT_START;     /* by default, stay in START */
  479.       inscript_state[INSCRIPT_START]['\\']=INSCRIPT_ANTISLASH;             /* #1: \ escapes the next character whatever it is */
  480.       inscript_state[INSCRIPT_ANTISLASH][INSCRIPT_DEFAULT]=INSCRIPT_START;
  481.       inscript_state[INSCRIPT_START]['\'']=INSCRIPT_INQUOTE;               /* #2: ' opens quote and only ' returns to 0 */
  482.       inscript_state[INSCRIPT_INQUOTE][INSCRIPT_DEFAULT]=INSCRIPT_INQUOTE;
  483.       inscript_state[INSCRIPT_INQUOTE]['\'']=INSCRIPT_START;
  484.       inscript_state[INSCRIPT_INQUOTE]['\\']=INSCRIPT_ANTISLASH_IN_QUOTE;
  485.       inscript_state[INSCRIPT_START]['\"']=INSCRIPT_INQUOTE2;              /* #3: " opens double-quote and only " returns to 0 */
  486.       inscript_state[INSCRIPT_INQUOTE2][INSCRIPT_DEFAULT]=INSCRIPT_INQUOTE2;
  487.       inscript_state[INSCRIPT_INQUOTE2]['\"']=INSCRIPT_START;
  488.       inscript_state[INSCRIPT_INQUOTE2]['\\']=INSCRIPT_ANTISLASH_IN_QUOTE2;
  489.       inscript_state[INSCRIPT_START]['/']=INSCRIPT_SLASH;                  /* #4: / state, default to #0 */
  490.       inscript_state[INSCRIPT_SLASH][INSCRIPT_DEFAULT]=INSCRIPT_START;
  491.       inscript_state[INSCRIPT_SLASH]['/']=INSCRIPT_SLASHSLASH;             /* #5: // with only LF to escape */
  492.       inscript_state[INSCRIPT_SLASHSLASH][INSCRIPT_DEFAULT]=INSCRIPT_SLASHSLASH;
  493.       inscript_state[INSCRIPT_SLASHSLASH]['\n']=INSCRIPT_START;
  494.       inscript_state[INSCRIPT_SLASH]['*']=INSCRIPT_COMMENT;                /* #6: / * with only * / to escape */
  495.       inscript_state[INSCRIPT_COMMENT][INSCRIPT_DEFAULT]=INSCRIPT_COMMENT;
  496.       inscript_state[INSCRIPT_COMMENT]['*']=INSCRIPT_COMMENT2;             /* #7: closing comments */
  497.       inscript_state[INSCRIPT_COMMENT2][INSCRIPT_DEFAULT]=INSCRIPT_COMMENT;
  498.       inscript_state[INSCRIPT_COMMENT2]['/']=INSCRIPT_START;
  499.       inscript_state[INSCRIPT_COMMENT2]['*']=INSCRIPT_COMMENT2;
  500.       inscript_state[INSCRIPT_ANTISLASH_IN_QUOTE][INSCRIPT_DEFAULT]=INSCRIPT_INQUOTE;    /* #8: escape in "" */
  501.       inscript_state[INSCRIPT_ANTISLASH_IN_QUOTE2][INSCRIPT_DEFAULT]=INSCRIPT_INQUOTE2;  /* #9: escape in '' */
  502.  
  503.       /* Primary list or URLs */
  504.       if (ptr == 0) {
  505.         intag=1;
  506.         intag_start_valid=0;
  507.                 intag_name = NULL;
  508.       }
  509.       /* Check is the file is a .js file */
  510.       else if (
  511.         (compare_mime(opt,r->contenttype, str->url_file, "application/x-javascript")!=0)
  512.         || (compare_mime(opt,r->contenttype, str->url_file, "text/css")!=0)
  513.         ) {      /* JavaScript js file */
  514.           inscript=1;
  515.           if (opt->parsedebug) { HT_ADD("<@@ inscript @@>"); }
  516.           inscript_name="script";
  517.           intag=1;     // because aprΦs <script> on y est .. - pas utile
  518.           intag_start_valid=0;    // OUI car nous sommes dans du code, plus dans du "vrai" tag
  519.           if ((opt->debug>1) && (opt->log!=NULL)) {
  520.             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"note: this file is a javascript file"LF); test_flush;
  521.           }
  522.           // for javascript only
  523.           if (compare_mime(opt,r->contenttype, str->url_file, "application/x-javascript") != 0) {
  524.             // all links must be checked against parent, not this link
  525.             if (liens[ptr]->precedent != 0) {
  526.               parent_relative=1;
  527.             }
  528.           }
  529.         }
  530.         /* Or a real audio */
  531.       else if (compare_mime(opt,r->contenttype, str->url_file, "audio/x-pn-realaudio")!=0) {      /* realaudio link file */
  532.         inscript=intag=0;
  533.         inscript_name="media";
  534.         intag_start_valid=0;
  535.         in_media="LNK";       // real media! -> links
  536.       } 
  537.       /* Or a m3u playlist */
  538.       else if (compare_mime(opt,r->contenttype, str->url_file, "audio/x-mpegurl")!=0) {      /* mp3 link file */
  539.         inscript=intag=0;
  540.         inscript_name="media";
  541.         intag_start_valid=0;
  542.         in_media="LNK";       // m3u! -> links
  543.       } 
  544.       else if (compare_mime(opt,r->contenttype, str->url_file, "application/x-authorware-map")!=0) {      /* macromedia aam file */
  545.         inscript=intag=0;
  546.         inscript_name="media";
  547.         intag_start_valid=0;
  548.         in_media="AAM";       // aam
  549.       } 
  550.       /* Or a RSS file */
  551.       else if (
  552.         compare_mime(opt,r->contenttype, str->url_file, "text/xml") != 0
  553.         || compare_mime(opt,r->contenttype, str->url_file, "application/xml") != 0
  554.         ) 
  555.       {
  556.         if (strstr(adr, "http://purl.org/rss/") != NULL) // Hmm, this is a bit lame ; will have to cleanup
  557.         {      /* RSS file */
  558.           inscript=intag=0;
  559.           intag_start_valid=0;
  560.           in_media=NULL;       // regular XML
  561.         } else {   // cancel: write all
  562.           adr = r->adr + r->size;
  563.           HT_ADD_ADR;
  564.           lastsaved=adr;
  565.         }
  566.       }
  567.  
  568.       // Detect UTF8 format
  569.       //if (is_unicode_utf8((unsigned char*) r->adr, (unsigned int) r->size) == 1) {
  570.       //  no_esc_utf=1;
  571.       //} else {
  572.       //  no_esc_utf=0;
  573.       //}
  574.  
  575.             // Hack to prevent any problems with ram files of other files
  576.       * ( r->adr + r->size ) = '\0';
  577.  
  578.       // ------------------------------------------------------------
  579.       // analyser ce qu'il y a en mΘmoire (fichier html)
  580.       // on scanne les balises
  581.       // ------------------------------------------------------------
  582.       opt->state._hts_in_html_done=0;     // 0% scannΘs
  583.       opt->state._hts_in_html_parsing=1;  // flag pour indiquer un parsing
  584.  
  585.             base[0]='\0';    // effacer base-href
  586.       do {
  587.         int p=0;
  588.         int valid_p=0;      // force to take p even if == 0
  589.         int ending_p='\0';  // ending quote?
  590.         int archivetag_p=0;  // avoid multiple-archives with commas
  591.         int  unquoted_script=0;
  592.         INSCRIPT inscript_state_pos_prev=inscript_state_pos;
  593.         error=0;
  594.  
  595.         /* Break if we are done yet */
  596.         if ( ( adr - r->adr ) >= r->size)
  597.           break;
  598.  
  599.         /* Hack to avoid NULL char problems with C syntax */
  600.         /* Yes, some bogus HTML pages can embed null chars
  601.         and therefore can not be properly handled if this hack is not done
  602.         */
  603.         if ( ! (*adr) ) {
  604.           if ( ((int) (adr - r->adr)) < r->size)
  605.             *adr=' ';
  606.         }
  607.  
  608.         /*
  609.         index.html built here
  610.         */
  611.         // Construction index.html (sommaire)
  612.         // Avant de tester les a href,
  613.         // Ici on teste si l'on doit construire l'index vers le(s) site(s) miroir(s)
  614.         if (!makeindex_done) {  // autoriation d'Θcrire un index
  615.           if (!detect_title) {
  616.             if (opt->depth == liens[ptr]->depth) {    // on note toujours les premiers liens
  617.               if (!in_media) {
  618.                 if (opt->makeindex && (ptr>0)) {
  619.                   if (opt->getmode & 1) {  // autorisation d'Θcrire
  620.                     p=strfield(adr,"title");  
  621.                     if (p) {
  622.                       if (*(adr-1)=='/') p=0;    // /title
  623.                     } else {
  624.                       if (strfield(adr,"/html"))
  625.                         p=-1;                    // noter, mais sans titre
  626.                       else if (strfield(adr,"body"))
  627.                         p=-1;                    // noter, mais sans titre
  628.                       else if ( ((int) (adr - r->adr) ) >= (r->size-1) )
  629.                         p=-1;                    // noter, mais sans titre
  630.                       else if ( (int) (adr - r->adr) >= r->size - 2)   // we got to hurry
  631.                         p=-1; // xxc xxc xxc
  632.                     }
  633.                   } else
  634.                     p=0;
  635.  
  636.                   if (p) {    // ok center                            
  637.                     if (makeindex_fp==NULL) {
  638.                       file_notify(opt,"", "", fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_html),"index.html"), 1, 1, 0);
  639.                       verif_backblue(opt,StringBuff(opt->path_html));    // gΘnΘrer gif
  640.                       makeindex_fp=filecreate(&opt->state.strc, fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_html),"index.html"));
  641.                       if (makeindex_fp!=NULL) {
  642.  
  643.                         // Header
  644.                         fprintf(makeindex_fp,template_header,
  645.                           "<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"
  646.                           );
  647.  
  648.                       } else makeindex_done=-1;    // fait, erreur
  649.                     }
  650.  
  651.                     if (makeindex_fp!=NULL) {
  652.                       char BIGSTK tempo[HTS_URLMAXSIZE*2];
  653.                       char BIGSTK s[HTS_URLMAXSIZE*2];
  654.                       char* a=NULL;
  655.                       char* b=NULL;
  656.                       s[0]='\0';
  657.                       if (p>0) {
  658.                         a=strchr(adr,'>');
  659.                         if (a!=NULL) {
  660.                           a++;
  661.                           while(is_space(*a)) a++;    // sauter espaces & co
  662.                           b=strchr(a,'<');   // prochain tag
  663.                         }
  664.                       }
  665.                       if (lienrelatif(tempo,liens[ptr]->sav,concat(OPT_GET_BUFF(opt),StringBuff(opt->path_html),"index.html"))==0) {
  666.                         detect_title=1;      // ok dΘtectΘ pour cette page!
  667.                         makeindex_links++;   // un de plus
  668.                         strcpybuff(makeindex_firstlink,tempo);
  669.                         //
  670.  
  671.                         /* Hack */
  672.                         if (opt->mimehtml) {
  673.                           strcpybuff(makeindex_firstlink, "cid:primary/primary");
  674.                         }
  675.  
  676.                         if ((b==a) || (a==NULL) || (b==NULL)) {    // pas de titre
  677.                           strcpybuff(s,tempo);
  678.                         } else if ((b-a)<256) {
  679.                           b--;
  680.                           while(is_space(*b)) b--;
  681.                           strncpy(s,a,b-a+1);
  682.                           *(s+(b-a)+1)='\0';
  683.                         }
  684.  
  685.                         // Body
  686.                         fprintf(makeindex_fp,template_body,
  687.                           tempo,
  688.                           s
  689.                           );
  690.  
  691.                       }
  692.                     }
  693.                   }
  694.                 }
  695.               }
  696.  
  697.             } else if (liens[ptr]->depth<opt->depth) {   // on a sautΘ level1+1 et level1
  698.               HT_INDEX_END;
  699.             }
  700.           } // if (opt->makeindex)
  701.         }
  702.         // FIN Construction index.html (sommaire)
  703.         /*
  704.         end -- index.html built here
  705.         */
  706.  
  707.  
  708.  
  709.         /* Parse */
  710.         if (
  711.           (*adr=='<')    /* No starting tag */
  712.           && (!inscript)    /* Not in (java)script */
  713.           && (!incomment)   /* Not in comment (<!--) */
  714.           && (!in_media)    /* Not in media */
  715.           ) { 
  716.             intag=1;
  717.                         intag_ctype=0;
  718.                         //parseall_incomment=0;
  719.                         //inquote=0;  // effacer quote
  720.                         intag_start = adr;
  721.                         for(intag_name = adr + 1 ; is_realspace(*intag_name) ; intag_name++ );
  722.                         intag_start_valid = 1;
  723.                         codebase[0]='\0';    // effacer Θventuel codebase
  724.  
  725.                         /* Meta ? */
  726.                         if (check_tag(intag_start, "meta")) {
  727.                             int pos;
  728.                             // <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  729.                             if ((pos = rech_tageq_all(adr, "http-equiv"))) {
  730.                                 const char* token = NULL;
  731.                                 int len = rech_endtoken(adr + pos, &token);
  732.                                 if (len > 0) {
  733.                                     if (strfield(token, "content-type")) {
  734.                                         intag_ctype=1;
  735.                                     }
  736.                                     else if (strfield(token, "refresh")) {
  737.                                         intag_ctype=2;
  738.                                     }
  739.                                 }
  740.                             }
  741.                         }
  742.  
  743.             if (opt->getmode & 1) {  // sauver html
  744.               p=strfield(adr,"</html");
  745.               if (p==0) p=strfield(adr,"<head>");
  746.               // if (p==0) p=strfield(adr,"<doctype");
  747.               if (p) {
  748.                 char* eol="\n";
  749.                 if (strchr(r->adr,'\r'))
  750.                   eol="\r\n";
  751.                 if (StringNotEmpty(opt->footer) || opt->urlmode != 4) {  /* != preserve */
  752.                                     if (StringNotEmpty(opt->footer)) {
  753.                       char BIGSTK tempo[1024+HTS_URLMAXSIZE*2];
  754.                                         char gmttime[256];
  755.                                         tempo[0]='\0';
  756.                                         time_gmt_rfc822(gmttime);
  757.                                         strcatbuff(tempo,eol);
  758.                       sprintf(tempo+strlen(tempo),StringBuff(opt->footer),jump_identification(urladr),urlfil,gmttime,HTTRACK_VERSIONID,"","","","","","","");
  759.                         strcatbuff(tempo,eol);
  760.                                         //fwrite(tempo,1,strlen(tempo),fp);
  761.                                         HT_ADD(tempo);
  762.                                     }
  763.                   if (strnotempty(r->charset)) {
  764.                     HT_ADD("<!-- Added by HTTrack --><meta http-equiv=\"content-type\" content=\"text/html;charset=");
  765.                     HT_ADD(r->charset);
  766.                     HT_ADD("\"><!-- /Added by HTTrack -->");
  767.                     HT_ADD(eol);
  768.                   }
  769.                 }
  770.               }
  771.             }        
  772.  
  773.             // Θliminer les <!-- (commentaires) : intag dΘvalidΘ
  774.             if (*(adr+1)=='!')
  775.               if (*(adr+2)=='-')
  776.                 if (*(adr+3)=='-') {
  777.                   intag=0;
  778.                   incomment=1;
  779.                   intag_start_valid=0;
  780.                 }
  781.  
  782.           }
  783.         else if (
  784.           (*adr=='>')                        /* ending tag */
  785.           && ( (!inscript && !in_media) || (inscript_tag) )  /* and in tag (or in script) */
  786.           ) {
  787.             if (inscript_tag) {
  788.               inscript_tag=inscript=0;
  789.               intag=0;
  790.               incomment=0;
  791.               intag_start_valid=0;
  792.                             intag_name = NULL;
  793.               if (opt->parsedebug) { HT_ADD("<@@ /inscript @@>"); }
  794.             } else if (!incomment) {
  795.               intag=0; //inquote=0;
  796.  
  797.               // entrΘe dans du javascript?
  798.               // on parse ICI car il se peut qu'on ait eu a parser les src=.. dedans
  799.               //if (!inscript) {  // sinon on est dans un obj.write("..
  800.               if ((intag_start_valid) && 
  801.                 (
  802.                 check_tag(intag_start,"script")
  803.                 ||
  804.                 check_tag(intag_start,"style")
  805.                 )
  806.                 ) {
  807.                   char* a=intag_start;    // <
  808.                   // ** while(is_realspace(*(--a)));
  809.                   if (*a=='<') {  // s√r que c'est un tag?
  810.                     if (check_tag(intag_start,"script"))
  811.                       inscript_name="script";
  812.                     else
  813.                       inscript_name="style";
  814.                     inscript=1;
  815.                     inscript_state_pos=INSCRIPT_START;
  816.                     intag=1;     // because aprΦs <script> on y est .. - pas utile
  817.                     intag_start_valid=0;    // OUI car nous sommes dans du code, plus dans du "vrai" tag
  818.                     if (opt->parsedebug) { HT_ADD("<@@ inscript @@>"); }
  819.                   }
  820.                 }
  821.             } else {                               /* end of comment? */
  822.               // vΘrifier fermeture correcte
  823.               if ( (*(adr-1)=='-') && (*(adr-2)=='-') ) {
  824.                 intag=0;
  825.                 incomment=0;
  826.                 intag_start_valid=0;
  827.                                 intag_name = NULL;
  828.               }
  829. #if GT_ENDS_COMMENT
  830.               /* wrong comment ending */
  831.               else {
  832.                 /* check if correct ending does not exists
  833.                 <!-- foo > example <!-- bar > is sometimes accepted by browsers
  834.                 when no --> is used somewhere else.. darn those browsers are dirty
  835.                 */
  836.                 if (!strstr(adr,"-->")) {
  837.                   intag=0;
  838.                   incomment=0;
  839.                   intag_start_valid=0;
  840.                                     intag_name = NULL;
  841.                 }
  842.               }
  843. #endif
  844.             }
  845.             //}
  846.           }
  847.           //else if (*adr==34) {
  848.           //  inquote=(inquote?0:1);
  849.           //}
  850.         else if (intag || inscript || in_media) {    // nous sommes dans un tag/commentaire, tester si on recoit un tag
  851.           int p_type=0;
  852.           int p_nocatch=0;
  853.           int p_searchMETAURL=0;  // chercher ..URL=<url>
  854.           int add_class=0;        // ajouter .class
  855.           int add_class_dots_to_patch=0;   // number of '.' in code="x.y.z<realname>"
  856.           char* p_flush=NULL;
  857.  
  858.  
  859.           // ------------------------------------------------------------
  860.           // parsing ΘvolΘ
  861.           // ------------------------------------------------------------
  862.           if (((isalpha((unsigned char)*adr)) || (*adr=='/') || (inscript) || (in_media) || (inscriptgen))) {  // sinon pas la peine de tester..
  863.  
  864.  
  865.             /* caractΦre de terminaison pour "miniparsing" javascript=.. ? 
  866.             (ex: <a href="javascript:()" action="foo"> ) */
  867.             if (inscript_tag) {
  868.               if (inscript_tag_lastc) {
  869.                 if (*adr == inscript_tag_lastc) {
  870.                   /* sortir */
  871.                   inscript_tag=inscript=0;
  872.                   incomment=0;
  873.                   if (opt->parsedebug) { HT_ADD("<@@ /inscript @@>"); }
  874.                 }
  875.               }
  876.             }
  877.  
  878.             /* automate */
  879.             AUTOMATE_LOOKUP_CURRENT_ADR();
  880.  
  881.  
  882.             // Note:
  883.             // Certaines pages ne respectent pas le html
  884.             // notamment les guillements ne sont pas fixΘs
  885.             // Nous sommes dans un tag, donc on peut faire un test plus
  886.             // large pour pouvoi prendre en compte ces particularitΘs
  887.  
  888.             // α vΘrifier: ACTION, CODEBASE, VRML
  889.  
  890.             if (in_media) {
  891.               if (strcmp(in_media,"LNK")==0) { // real media
  892.                 p=0;
  893.                 valid_p=1;
  894.               }
  895.               else if (strcmp(in_media, "AAM")==0) { // AAM
  896.                 if (is_space((unsigned char)adr[0]) && ! is_space((unsigned char)adr[1])) {
  897.                   char* a = adr + 1;
  898.                   int n = 0;
  899.                   int ok = 0;
  900.                   int dot = 0;
  901.                   while(n < HTS_URLMAXSIZE/2 && a[n] != '\0' &&
  902.                     ( ! is_space((unsigned char)a[n]) || ! ( ok = 1) )
  903.                     ) {
  904.                       if (a[n] == '.') {
  905.                         dot = n;
  906.                       }
  907.                       n++;
  908.                     }
  909.                     if (ok && dot > 0) {
  910.                       char BIGSTK tmp[HTS_URLMAXSIZE/2 + 2];
  911.                       tmp[0] = '\0';
  912.                       strncat(tmp, a + dot + 1, n - dot - 1);
  913.                       if (is_knowntype(opt,tmp) || ishtml_ext(tmp) != -1) {
  914.                         adr++;
  915.                         p = 0;
  916.                         valid_p = 1;
  917.                         unquoted_script = 1;
  918.                       }
  919.                     }
  920.                 }
  921.               }
  922.             } else if (ptr>0) {        /* pas premiΦre page 0 (primary) */
  923.               p=0;  // saut pour le nom de fichier: adresse nom fichier=adr+p
  924.  
  925.               // ------------------------------
  926.               // dΘtection d'Θcriture JavaScript.
  927.               // osons les obj.write et les obj.href=.. ! osons!
  928.               // note: inscript==1 donc on sautera aprΦs les \"
  929.               if (inscript) {
  930.                 if (inscriptgen) {          // on est dΘja dans un objet gΘnΘrant..
  931.                   if (*adr==scriptgen_q) {  // fermeture des " ou '
  932.                     if (*(adr-1)!='\\') {   // non
  933.                       inscriptgen=0;        // ok parsing terminΘ
  934.                     }
  935.                   }
  936.                 } else {
  937.                   char* a=NULL;
  938.                   char check_this_fking_line=0;  // parsing code javascript..
  939.                   char must_be_terminated=0;     // caractΦre obligatoire de terminaison!
  940.                   int token_size;
  941.                   if (!(token_size=strfield(adr,".writeln"))) // dΘtection ...objet.write[ln]("code html")...
  942.                     token_size=strfield(adr,".write");
  943.                   if (token_size) {
  944.                     a=adr+token_size;
  945.                     while(is_realspace(*a)) a++; // sauter espaces
  946.                     if (*a=='(') {  // dΘbut parenthΦse
  947.                       check_this_fking_line=2;  // α parser!
  948.                       must_be_terminated=')';
  949.                       a++;  // sauter (
  950.                     }
  951.                   }
  952.                   // euhh ??? ???
  953.                   /* else if (strfield(adr,".href")) {  // dΘtection ...objet.href="...
  954.                   a=adr+5;
  955.                   while(is_realspace(*a)) a++; // sauter espaces
  956.                   if (*a=='=') {  // ohh un Θgal
  957.                   check_this_fking_line=1;  // α noter!
  958.                   must_be_terminated=';';   // et si t'as oubliΘ le ; tu sais pas coder
  959.                   a++;   // sauter =
  960.                   }
  961.  
  962.                   }*/
  963.  
  964.                   // on a un truc du genre instruction"code gΘnΘrΘ" dont on parse le code
  965.                   if (check_this_fking_line) {
  966.                     while(is_realspace(*a)) a++;
  967.                     if ((*a=='\'') || (*a=='"')) {  // dΘpart de '' ou ""
  968.                       char *b;
  969.                       scriptgen_q=*a;    // quote
  970.                       b=a+1;      // dΘpart de la chaεne
  971.                       // vΘrifier forme ("code") et pas ("code"+var), ingΘrable
  972.                       do {
  973.                         if (*a==scriptgen_q && *(a-1)!='\\')  // quote non slash
  974.                           break;            // sortie
  975.                         else if (*a==10 && *(a-1) != '\\'  /* LF and no continue (\) character */
  976.                           && ( *(a-1) != '\r' || *(a-2) != '\\' ) )  /* and not CRLF and no .. */
  977.                           break;
  978.                         else 
  979.                           a++;  // caractΦre suivant
  980.                       } while((a-b) < HTS_URLMAXSIZE / 2);
  981.                       if (*a==scriptgen_q) {  // fin du quote
  982.                         a++;
  983.                         while(is_realspace(*a)) a++;
  984.                         if (*a==must_be_terminated) {  // parenthΦse fermante: ("..")
  985.  
  986.                           // bon, on doit parser une ligne javascript
  987.                           // 1) si check.. ==1 alors c'est un nom de fichier direct, donc
  988.                           // on fixe p sur le saut nΘcessaire pour atteindre le nom du fichier
  989.                           // et le moteur se dΘbrouillera ensuite tout seul comme un grand
  990.                           // 2) si check==2 c'est un peu plus tordu car lα on gΘnΘre du
  991.                           // code html au sein de code javascript au sein de code html
  992.                           // dans ce cas on doit fixer un flag α un puis ensuite dans la boucle
  993.                           // on devra parser les instructions standard comme <a href etc
  994.                           // NOTE: le code javascript autogΘnΘrΘ n'est pas pris en compte!!
  995.                           // (et ne marche pas dans 50% des cas de toute facon!)
  996.                           if (check_this_fking_line==1) {
  997.                             p=(int) (b - adr);    // calculer saut!
  998.                           } else {
  999.                             inscriptgen=1;        // SCRIPTGEN actif
  1000.                             adr=b;                // jump
  1001.                           }
  1002.  
  1003.                           if ((opt->debug>1) && (opt->log!=NULL)) {
  1004.                             char str[512];
  1005.                             str[0]='\0';
  1006.                             strncatbuff(str,b,minimum((int) (a - b + 1), 32));
  1007.                             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"active code (%s) detected in javascript: %s"LF,(check_this_fking_line==2)?"parse":"pickup",str); test_flush;
  1008.                           }
  1009.                         }
  1010.  
  1011.                       }
  1012.  
  1013.                     }
  1014.  
  1015.  
  1016.                   }
  1017.                 }
  1018.               }
  1019.               // fin detection code gΘnΘrant javascript vers html
  1020.               // ------------------------------
  1021.  
  1022.  
  1023.               // analyse proprement dite, A HREF=.. etc..
  1024.               if (!p) {
  1025.                 // si dans un tag, et pas dans un script - sauf si on analyse un obj.write("..
  1026.                 if ((intag && (!inscript)) || inscriptgen) {
  1027.                   if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  1028.                     // <A HREF=.. pour les liens HTML
  1029.                     p=rech_tageq(adr,"href");
  1030.                     if (p) {    // href.. tester si c'est une bas href!
  1031.                       if ((intag_start_valid) && check_tag(intag_start, "base")) {  // oui!
  1032.                         // ** note: base href et codebase ne font pas bon mΘnage..
  1033.                         p_type=2;    // c'est un chemin
  1034.                       }
  1035.                     }
  1036.  
  1037.                     /* Tags supplΘmentaires α vΘrifier (<img src=..> etc) */
  1038.                     if (p==0) {
  1039.                       int i=0;
  1040.                       while( (p==0) && (strnotempty(hts_detect[i])) ) {
  1041.                         p=rech_tageq(adr,hts_detect[i]);
  1042.                         if (p) {
  1043.                           /* This is a temporary hack to avoid archive=foo.jar,bar.jar .. */
  1044.                           if (strcmp(hts_detect[i], "archive") == 0) {
  1045.                             archivetag_p = 1;
  1046.                           }
  1047.                         }
  1048.                         i++;
  1049.                       }
  1050.                     }
  1051.  
  1052.                     /* Tags supplΘmentaires en dΘbut α vΘrifier (<object .. hotspot1=..> etc) */
  1053.                     if (p==0) {
  1054.                       int i=0;
  1055.                       while( (p==0) && (strnotempty(hts_detectbeg[i])) ) {
  1056.                         p=rech_tageqbegdigits(adr,hts_detectbeg[i]);
  1057.                         i++;
  1058.                       }
  1059.                     }
  1060.  
  1061.                     /* Tags supplΘmentaires α vΘrifier : URL=.. */
  1062.                     if (p==0) {
  1063.                       int i=0;
  1064.                       while( (p==0) && (strnotempty(hts_detectURL[i])) ) {
  1065.                         p=rech_tageq(adr,hts_detectURL[i]);
  1066.                         i++;
  1067.                       }
  1068.                       if (p) {
  1069.                         if (intag_ctype == 1) {
  1070.                           p = 0;
  1071. #if 0
  1072.                           //if ((pos=rech_tageq(adr, "content"))) {
  1073.                           char temp[256];
  1074.                           char* token = NULL;
  1075.                           int len = rech_endtoken(adr + pos, &token);
  1076.                           if (len > 0 && len < sizeof(temp) - 2) {
  1077.                             char* chpos;
  1078.                             temp[0] = '\0';
  1079.                             strncat(temp, token, len);
  1080.                             if ((chpos = strstr(temp, "charset"))
  1081.                               &&
  1082.                               (chpos = strchr(chpos, '='))
  1083.                               ) {
  1084.                                 chpos++;
  1085.                                 while(is_space(*chpos)) chpod++;
  1086.                                 chpos
  1087.                               }
  1088.                           }
  1089. #endif
  1090.                         }
  1091.                         // <META HTTP-EQUIV="Refresh" CONTENT="3;URL=http://www.example.com">
  1092.                         else if (intag_ctype == 2) {
  1093.                           p_searchMETAURL=1;
  1094.                         } else {
  1095.                           p = 0;            /* cancel */
  1096.                         }
  1097.                       }
  1098.  
  1099.  
  1100.                     }
  1101.  
  1102.                     /* Tags supplΘmentaires α vΘrifier, mais α ne pas capturer */
  1103.                     if (p==0) {
  1104.                       int i=0;
  1105.                       while( (p==0) && (strnotempty(hts_detectandleave[i])) ) {
  1106.                         p=rech_tageq(adr,hts_detectandleave[i]);
  1107.                         i++;
  1108.                       }
  1109.                       if (p)
  1110.                         p_nocatch=1;      /* ne pas rechercher */
  1111.                     }
  1112.  
  1113.                     /* EvΘnements */
  1114.                     if (p==0 && 
  1115.                       ! inscript          /* we don't want events inside document.write */
  1116.                       ) {
  1117.                         int i=0;
  1118.                         /* dΘtection onLoad etc */
  1119.                         while( (p==0) && (strnotempty(hts_detect_js[i])) ) {
  1120.                           p=rech_tageq(adr,hts_detect_js[i]);
  1121.                           i++;
  1122.                         }
  1123.                         /* non dΘtectΘ - dΘtecter Θgalement les onXxxxx= */
  1124.                         if (p==0) {
  1125.                           if ( (*adr=='o') && (*(adr+1)=='n') && isUpperLetter(*(adr+2)) ) {
  1126.                             p=0;
  1127.                             while(isalpha((unsigned char)adr[p]) && (p<64) ) p++;
  1128.                             if (p<64) {
  1129.                               while(is_space(adr[p])) p++;
  1130.                               if (adr[p]=='=')
  1131.                                 p++;
  1132.                               else p=0;
  1133.                             } else p=0;
  1134.                           }
  1135.                         }
  1136.                         /* OK, ΘvΘnement repΘrΘ */
  1137.                         if (p) {
  1138.                           inscript_tag_lastc=*(adr+p);     /* α attendre α la fin */
  1139.                           adr+=p+1;   /* saut */
  1140.                           /*
  1141.                           On est dΘsormais dans du code javascript
  1142.                           */
  1143.                           inscript_name="";
  1144.                           inscript=inscript_tag=1;
  1145.                           inscript_state_pos=INSCRIPT_START;
  1146.                           if (opt->parsedebug) { HT_ADD("<@@ inscript @@>"); }
  1147.                         }
  1148.                         p=0;        /* quoi qu'il arrive, ne rien dΘmarrer ici */
  1149.                       }
  1150.  
  1151.                       // <APPLET CODE=.. pour les applet java.. [CODEBASE (chemin..) α faire]
  1152.                       if (p==0) {
  1153.                         p=rech_tageq(adr,"code");
  1154.                         if (p) {
  1155.                           if ((intag_start_valid) && check_tag(intag_start,"applet")) {  // dans un <applet !
  1156.                             p_type=-1;  // juste le nom de fichier+dossier, Θcire avant codebase 
  1157.                             add_class=1;   // ajouter .class au besoin                         
  1158.  
  1159.                             // vΘrifier qu'il n'y a pas de codebase APRES
  1160.                             // sinon on swappe les deux.
  1161.                             // pas trΦs propre mais c'est ce qu'il y a de plus simple α faire!!
  1162.  
  1163.                             {
  1164.                               char *a;
  1165.                               a=adr;
  1166.                               while((*a) && (*a!='>') && (!rech_tageq(a,"codebase"))) a++;
  1167.                               if (rech_tageq(a,"codebase")) {  // banzai! codebase=
  1168.                                 char* b;
  1169.                                 b=strchr(a,'>');
  1170.                                 if (b) {
  1171.                                   if (((int) (b - adr)) < 1000) {    // au total < 1Ko
  1172.                                     char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1173.                                     tempo[0]='\0';
  1174.                                     strncatbuff(tempo,a,(int) (b - a) );
  1175.                                     strcatbuff( tempo," ");
  1176.                                     strncatbuff(tempo,adr,(int) (a - adr - 1));
  1177.                                     // Θventuellement remplire par des espaces pour avoir juste la taille
  1178.                                     while((int) strlen(tempo)<((int) (b - adr)))
  1179.                                       strcatbuff(tempo," ");
  1180.                                     // pas d'erreur?
  1181.                                     if ((int) strlen(tempo) == ((int) (b - adr) )) {
  1182.                                       strncpy(adr,tempo,strlen(tempo));   // PAS d'octet nul α la fin!
  1183.                                       p=0;    // DEVALIDER!!
  1184.                                       p_type=0;
  1185.                                       add_class=0;
  1186.                                     }
  1187.                                   }
  1188.                                 }
  1189.                               }
  1190.                             }
  1191.  
  1192.                           }
  1193.                         }
  1194.                       }
  1195.  
  1196.                       // liens α patcher mais pas α charger (ex: codebase)
  1197.                       if (p==0) {  // note: si non chargΘ (ex: ignorer .class) patchΘ tout de mΩme
  1198.                         p=rech_tageq(adr,"codebase");
  1199.                         if (p) {
  1200.                           if ((intag_start_valid) && check_tag(intag_start,"applet")) {  // dans un <applet !
  1201.                             p_type=-2;
  1202.                           } else p=-1;   // ne plus chercher
  1203.                         }
  1204.                       }
  1205.  
  1206.  
  1207.                       // Meta tags pour robots
  1208.                       if (p==0) {
  1209.                         if (opt->robots) {
  1210.                           if ((intag_start_valid) && check_tag(intag_start,"meta")) {
  1211.                             if (rech_tageq(adr,"name")) {    // name=robots.txt
  1212.                               char tempo[1100];
  1213.                               char* a;
  1214.                               tempo[0]='\0';
  1215.                               a=strchr(adr,'>');
  1216. #if DEBUG_ROBOTS
  1217.                               printf("robots.txt meta tag detected\n");
  1218. #endif
  1219.                               if (a) {
  1220.                                 if (((int) (a - adr)) < 999 ) {
  1221.                                   strncatbuff(tempo,adr,(int) (a - adr));
  1222.                                   if (strstrcase(tempo,"content")) {
  1223.                                     if (strstrcase(tempo,"robots")) {
  1224.                                       if (strstrcase(tempo,"nofollow")) {
  1225. #if DEBUG_ROBOTS
  1226.                                         printf("robots.txt meta tag: nofollow in %s%s\n",urladr,urlfil);
  1227. #endif
  1228.                                         nofollow=1;       // NE PLUS suivre liens dans cette page
  1229.                                         if (opt->log) {
  1230.                                           HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Link %s%s not scanned (follow robots meta tag)"LF,urladr,urlfil);
  1231.                                           test_flush;
  1232.                                         }
  1233.                                       }
  1234.                                     }
  1235.                                   }
  1236.                                 }
  1237.                               }
  1238.                             }
  1239.                           }
  1240.                         }
  1241.                       }
  1242.  
  1243.                                             // entrΘe dans une applet javascript
  1244.                       /*if (!inscript) {  // sinon on est dans un obj.write("..
  1245.                       if (p==0)
  1246.                       if (rech_sampletag(adr,"script"))
  1247.                       if (check_tag(intag_start,"script")) {
  1248.                       inscript=1;
  1249.                       }
  1250.                       }*/
  1251.  
  1252.                       // Ici on procΦde α une analyse du code javascript pour tenter de rΘcupΘrer
  1253.                       // certains fichiers Θvidents.
  1254.                       // C'est devenu obligatoire vu le nombre de pages qui intΦgrent
  1255.                       // des images rΘactives par exemple
  1256.                   }
  1257.                 } else if (inscript) {
  1258.  
  1259. #if 0
  1260.                   /* Check // javascript comments */
  1261.                   if (*adr == 10 || *adr == 13) {
  1262.                     inscript_check_comments = 1;
  1263.                     inscript_in_comments = 0;
  1264.                   }
  1265.                   else if (inscript_check_comments) {
  1266.                     if (!is_realspace(*adr)) {
  1267.                       inscript_check_comments = 0;
  1268.                       if (adr[0] == '/' && adr[1] == '/') {
  1269.                         inscript_in_comments = 1;
  1270.                       }
  1271.                     }
  1272.                   }
  1273. #endif
  1274.  
  1275.                   /* Parse */
  1276.                   assertf(inscript_name != NULL);
  1277.                   if (
  1278.                     *adr == '/' &&
  1279.                     (
  1280.                     (strfield(adr,"/script") && strfield(inscript_name, "script"))
  1281.                     ||
  1282.                     (strfield(adr,"/style")  && strfield(inscript_name, "style"))
  1283.                     )
  1284.                     ) {
  1285.                       char* a=adr;
  1286.                       //while(is_realspace(*(--a)));
  1287.                       while( is_realspace(*a) ) a--;
  1288.                       a--;
  1289.                       if (*a=='<') {  // s√r que c'est un tag?
  1290.                         inscript=0;
  1291.                         if (opt->parsedebug) { HT_ADD("<@@ /inscript @@>"); }
  1292.                       }
  1293.                     } else if (inscript_state_pos == INSCRIPT_START /*!inscript_in_comments*/) {
  1294.                       /*
  1295.                       Script Analyzing - different types supported:
  1296.                       foo="url"
  1297.                       foo("url") or foo(url)
  1298.                       foo "url"
  1299.                       */
  1300.                       char  expected     = '=';          // caractΦre attendu aprΦs
  1301.                       char* expected_end = ";";
  1302.                       int can_avoid_quotes=0;
  1303.                       char quotes_replacement='\0';
  1304.                       int ensure_not_mime=0;
  1305.                       if (inscript_tag)
  1306.                         expected_end=";\"\'";            // voir a href="javascript:doc.location='foo'"
  1307.  
  1308.                       /* Can we parse javascript ? */
  1309.                       if ( (opt->parsejava & HTSPARSE_NO_JAVASCRIPT) == 0) {
  1310.                         int nc;
  1311.                         nc = strfield(adr,".src");  // nom.src="image";
  1312.                         if (!nc) nc = strfield(adr,".location");  // document.location="doc"
  1313.                         if (!nc) nc = strfield(adr,":location");  // javascript:location="doc"
  1314.                         if (!nc) { // location="doc"
  1315.                           if ( ( nc = strfield(adr,"location") ) 
  1316.                             && !isspace(*(adr - 1))
  1317.                             )
  1318.                             nc = 0;
  1319.                         }
  1320.                         if (!nc) nc = strfield(adr,".href");  // document.location="doc"
  1321.                         if (!nc) if ( (nc = strfield(adr,".open")) ) { // window.open("doc",..
  1322.                           expected='(';    // parenthΦse
  1323.                           expected_end="),";  // fin: virgule ou parenthΦse
  1324.                           ensure_not_mime=1;  //* ensure the url is not a mime type */
  1325.                         }
  1326.                         if (!nc) if ( (nc = strfield(adr,".replace")) ) { // window.replace("url")
  1327.                           expected='(';    // parenthΦse
  1328.                           expected_end=")";  // fin: parenthΦse
  1329.                         }
  1330.                         if (!nc) if ( (nc = strfield(adr,".link")) ) { // window.link("url")
  1331.                           expected='(';    // parenthΦse
  1332.                           expected_end=")";  // fin: parenthΦse
  1333.                         }
  1334.                         if (!nc) if ( (nc = strfield(adr,"url")) && (!isalnum(*(adr - 1))) 
  1335.                           && *(adr - 1) != '_'
  1336.                           ) { // url(url)
  1337.                             expected='(';    // parenthΦse
  1338.                             expected_end=")";  // fin: parenthΦse
  1339.                             can_avoid_quotes=1;
  1340.                             quotes_replacement=')';
  1341.                           }
  1342.                           if (!nc) if ( (nc = strfield(adr,"import")) ) { // import "url"
  1343.                             if (is_space(*(adr+nc))) {
  1344.                               expected=0;    // no char expected
  1345.                             } else
  1346.                               nc=0;
  1347.                           }
  1348.                           if (nc) {
  1349.                             char *a;
  1350.                             a=adr+nc;
  1351.                             while(is_realspace(*a)) a++;
  1352.                             if ((*a == expected) || (!expected)) {
  1353.                               if (expected)
  1354.                                 a++;
  1355.                               while(is_realspace(*a)) a++;
  1356.                               if ((*a==34) || (*a=='\'') || (can_avoid_quotes)) {
  1357.                                 char *b,*c;
  1358.                                 int ndelim=1;
  1359.                                 if ((*a==34) || (*a=='\''))
  1360.                                   a++;
  1361.                                 else
  1362.                                   ndelim=0;
  1363.                                 b=a;
  1364.                                 if (ndelim) {
  1365.                                   while((*b!=34) && (*b!='\'') && (*b!='\0')) b++;
  1366.                                 }
  1367.                                 else {
  1368.                                   while((*b != quotes_replacement) && (*b!='\0')) b++;
  1369.                                 }
  1370.                                 c=b--; c+=ndelim;
  1371.                                 while(*c==' ') c++;
  1372.                                 if ((strchr(expected_end,*c)) || (*c=='\n') || (*c=='\r')) {
  1373.                                   c-=(ndelim+1);
  1374.                                   if ((int) (c - a + 1)) {
  1375.                                     if (ensure_not_mime) {
  1376.                                       int i = 0;
  1377.                                       while(a != NULL && hts_main_mime[i] != NULL && hts_main_mime[i][0] != '\0') {
  1378.                                         int p;
  1379.                                         if ((p=strfield(a, hts_main_mime[i])) && a[p] == '/') {
  1380.                                           a=NULL;
  1381.                                         }
  1382.                                         i++;
  1383.                                       }
  1384.                                     }
  1385.                                     if (a != NULL) {
  1386.                                       if ((opt->debug>1) && (opt->log!=NULL)) {
  1387.                                         char str[512];
  1388.                                         str[0]='\0';
  1389.                                         strncatbuff(str,a,minimum((int) (c - a + 1),32));
  1390.                                         HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link detected in javascript: %s"LF,str); test_flush;
  1391.                                       }
  1392.                                       p=(int) (a - adr);    // p non nul: TRAITER CHAINE COMME FICHIER
  1393.                                       if (can_avoid_quotes) {
  1394.                                         ending_p=quotes_replacement;
  1395.                                       }
  1396.                                     }
  1397.                                   }
  1398.                                 }
  1399.  
  1400.  
  1401.                               }
  1402.                             }
  1403.                           }
  1404.  
  1405.                       }  /* HTSPARSE_NO_JAVASCRIPT */
  1406.  
  1407.                     }
  1408.                 }
  1409.               }
  1410.  
  1411.             } else {      // ptr == 0
  1412.               //p=rech_tageq(adr,"primary");    // lien primaire, yeah
  1413.               p=0;          // No stupid tag anymore, raw link
  1414.               valid_p=1;    // Valid even if p==0
  1415.               while ((adr[p] == '\r') || (adr[p] == '\n'))
  1416.                 p++;
  1417.               //can_avoid_quotes=1;
  1418.               ending_p='\r';
  1419.             }       
  1420.  
  1421.           } else if (isspace((unsigned char)*adr)) {
  1422.             intag_startattr=adr+1;        // attribute in tag (for dirty parsing)
  1423.           }
  1424.  
  1425.  
  1426.           // ------------------------------------------------------------
  1427.           // dernier recours - parsing "sale" : dΘtection systΘmatique des .gif, etc.
  1428.           // risque: gΘnΘrer de faux fichiers parazites
  1429.           // fix: ne parse plus dans les commentaires
  1430.           // ------------------------------------------------------------
  1431.           if ( opt->parseall && (opt->parsejava & HTSPARSE_NO_AGGRESSIVE) == 0 
  1432.             && (ptr>0) && (!in_media) /* && (!inscript_in_comments)*/ ) {   // option parsing "brut"
  1433.             //int incomment_justquit=0;
  1434.             if (!is_realspace(*adr)) {
  1435.               int noparse=0;
  1436.  
  1437.               // Gestion des /* */
  1438. #if 0
  1439.               if (inscript) {
  1440.                 if (parseall_incomment) {
  1441.                   if ((*adr=='/') && (*(adr-1)=='*'))
  1442.                     parseall_incomment=0;
  1443.                   incomment_justquit=1;       // ne pas noter dernier caractΦre
  1444.                 } else {
  1445.                   if ((*adr=='/') && (*(adr+1)=='*'))
  1446.                     parseall_incomment=1;
  1447.                 }
  1448.               } else
  1449.                 parseall_incomment=0;
  1450. #endif
  1451.               /* ensure automate state  0 (not in comments, quotes..) */
  1452.               if (inscript && ( 
  1453.                 inscript_state_pos != INSCRIPT_INQUOTE && inscript_state_pos != INSCRIPT_INQUOTE2
  1454.                 ) ) {
  1455.                   noparse=1;
  1456.                 }
  1457.  
  1458.                 /* vΘrifier que l'on est pas dans un <!-- --> pur */
  1459.                 if ( (!intag) && (incomment) && (!inscript))
  1460.                   noparse=1;        /* commentaire */
  1461.  
  1462.                 // recherche d'URLs
  1463.                 if (!noparse) {
  1464.                   //if ((!parseall_incomment) && (!noparse)) {
  1465.                   if (!p) {                   // non dΘja trouvΘ
  1466.                     if (adr != r->adr) {     // >1 caractΦre
  1467.                       // scanner les chaines
  1468.                       if ((*adr == '\"') || (*adr=='\'')) {         // "xx.gif" 'xx.gif'
  1469.                         if (strchr("=(,",parseall_lastc)) {    // exemple: a="img.gif.. (handles comments)
  1470.                           char *a=adr;
  1471.                           char stop=*adr;  // " ou '
  1472.                           int count=0;
  1473.  
  1474.                           // sauter caractΦres
  1475.                           a++;
  1476.                           // copier
  1477.                           while((*a) && (*a!='\'') && (*a!='\"') && (count<HTS_URLMAXSIZE)) { count++; a++; }
  1478.  
  1479.                           // ok chaine terminΘe par " ou '
  1480.                           if ((*a == stop) && (count<HTS_URLMAXSIZE) && (count>0)) {
  1481.                             char c;
  1482.                             char* aend;
  1483.                             //
  1484.                             aend=a;     // sauver dΘbut
  1485.                             a++;
  1486.                             while(is_taborspace(*a)) a++;
  1487.                             c=*a;
  1488.                             if (strchr("),;>/+\r\n",c)) {     // exemple: ..img.gif";
  1489.                               // le / est pour funct("img.gif" /* URL */);
  1490.                               char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1491.                               char type[256];
  1492.                               int url_ok=0;      // url valide?
  1493.                               tempo[0]='\0'; type[0]='\0';
  1494.                               //
  1495.                               strncatbuff(tempo,adr+1,count);
  1496.                               //
  1497.                               if ((!strchr(tempo,' ')) || inscript) {   // espace dedans: mΘfiance! (sauf dans code javascript)
  1498.                                 int invalid_url=0;
  1499.  
  1500.                                 // escape                              
  1501.                                 unescape_amp(tempo);
  1502.  
  1503.                                 // Couper au # ou ? Θventuel
  1504.                                 {
  1505.                                   char* a=strchr(tempo,'#');
  1506.                                   if (a)
  1507.                                     *a='\0';
  1508.                                   a=strchr(tempo,'?');
  1509.                                   if (a)
  1510.                                     *a='\0';
  1511.                                 }
  1512.  
  1513.                                 // vΘrifier qu'il n'y a pas de caractΦres spΘciaux
  1514.                                 if (!strnotempty(tempo))
  1515.                                   invalid_url=1;
  1516.                                 else if (strchr(tempo,'*')
  1517.                                   || strchr(tempo,'<')
  1518.                                   || strchr(tempo,'>')
  1519.                                   || strchr(tempo,',')    /* list of files ? */
  1520.                                   || strchr(tempo,'\"')    /* potential parsing bug */
  1521.                                   || strchr(tempo,'\'')    /* potential parsing bug */
  1522.                                   )
  1523.                                   invalid_url=1;
  1524.                                 else if (tempo[0] == '.' && isalnum(tempo[1]))   // ".gif"
  1525.                                   invalid_url=1;
  1526.  
  1527.                                 /* non invalide? */
  1528.                                 if (!invalid_url) {
  1529.                                   // Un plus α la fin? Alors ne pas prendre sauf si extension ("/toto.html#"+tag)
  1530.                                   if (c!='+') {    // PAS de plus α la fin
  1531. #if 0
  1532.                                     char* a;
  1533. #endif
  1534.                                     // "Comparisons of scheme names MUST be case-insensitive" (RFC2616)                                  
  1535.                                     if (
  1536.                                       (strfield(tempo,"http:")) 
  1537.                                       || (strfield(tempo,"ftp:"))
  1538. #if HTS_USEOPENSSL
  1539.                                       || (
  1540.                                       SSL_is_available &&
  1541.                                       (strfield(tempo,"https:"))
  1542.                                       )
  1543. #endif
  1544. #if HTS_USEMMS
  1545.                                                                             || strfield(tempo,"mms:")
  1546. #endif
  1547.                                       )  // ok pas de problΦme
  1548.                                       url_ok=1;
  1549.                                     else if (tempo[strlen(tempo)-1]=='/') {        // un slash: ok..
  1550.                                       if (inscript)   // sinon si pas javascript, mΘfiance (rΘpertoire style base?)
  1551.                                         url_ok=1;
  1552.                                     } 
  1553. #if 0
  1554.                                     else if ((a=strchr(tempo,'/'))) {        // un slash: ok..
  1555.                                       if (inscript) {    // sinon si pas javascript, mΘfiance (style "text/css")
  1556.                                         if (strchr(a+1,'/'))     // un seul / : abandon (STYLE type='text/css')
  1557.                                           if (!strchr(tempo,' '))  // avoid spaces (too dangerous for comments)
  1558.                                             url_ok=1;
  1559.                                       }
  1560.                                     }
  1561. #endif
  1562.                                   }
  1563.                                   // Prendre si extension reconnue
  1564.                                   if (!url_ok) {
  1565.                                     get_httptype(opt,type,tempo,0);
  1566.                                     if (strnotempty(type))     // type reconnu!
  1567.                                       url_ok=1;
  1568.                                     else if (is_dyntype(get_ext(OPT_GET_BUFF(opt),tempo)))  // reconnu php,cgi,asp..
  1569.                                       url_ok=1;
  1570.                                     // MAIS pas les foobar@aol.com !!
  1571.                                     if (strchr(tempo,'@'))
  1572.                                       url_ok=0;
  1573.                                   }
  1574.                                   //
  1575.                                   // Ok, cela pourrait Ωtre une URL
  1576.                                   if (url_ok) {
  1577.  
  1578.                                     // Check if not fodbidden tag (id,name..)
  1579.                                     if (intag_start_valid) {
  1580.                                       if (intag_start)
  1581.                                         if (intag_startattr)
  1582.                                           if (intag)
  1583.                                             if (!inscript)
  1584.                                               if (!incomment) {
  1585.                                                 int i=0,nop=0;
  1586.                                                 while( (nop==0) && (strnotempty(hts_nodetect[i])) ) {
  1587.                                                   nop=rech_tageq(intag_startattr,hts_nodetect[i]);
  1588.                                                   i++;
  1589.                                                 }
  1590.                                                 // Forbidden tag
  1591.                                                 if (nop) {
  1592.                                                   url_ok=0;
  1593.                                                   if ((opt->debug>1) && (opt->log!=NULL)) {
  1594.                                                     HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"dirty parsing: bad tag avoided: %s"LF,hts_nodetect[i-1]); test_flush;
  1595.                                                   }
  1596.                                                 }
  1597.                                               }
  1598.                                     }
  1599.  
  1600.  
  1601.                                     // Accepter URL, on la traitera comme une URL normale!!
  1602.                                     if (url_ok) {
  1603.                                       valid_p = 1;
  1604.                                       p = 0;
  1605.                                     }
  1606.  
  1607.                                   }
  1608.                                 }
  1609.                               }
  1610.                             }
  1611.                           }
  1612.                         }
  1613.                       }
  1614.                     }
  1615.                   }  // p == 0               
  1616.  
  1617.                 } // not in comment
  1618.  
  1619.                 // plus dans un commentaire
  1620.                 if ( inscript_state_pos == INSCRIPT_START 
  1621.                   && inscript_state_pos_prev == INSCRIPT_START) {
  1622.                     parseall_lastc=*adr;             // caractΦre avant le prochain
  1623.                   }
  1624.  
  1625.  
  1626.             }  // if realspace
  1627.           }  // if parseall
  1628.  
  1629.  
  1630.           // ------------------------------------------------------------
  1631.           // p!=0 : on a repΘrΘ un Θventuel lien
  1632.           // ------------------------------------------------------------
  1633.           //
  1634.           if ((p>0) || (valid_p)) {    // on a repΘrΘ un lien
  1635.             //int lien_valide=0;
  1636.             char* eadr=NULL;          /* fin de l'URL */
  1637.             char* quote_adr=NULL;     /* adresse du ? dans l'adresse */
  1638.             int ok=1;
  1639.             char quote='\0';
  1640.             int quoteinscript=0;
  1641.             int  noquote=0;
  1642.                         char *tag_attr_start = adr;
  1643.  
  1644.             // si nofollow ou un stop a ΘtΘ dΘclenchΘ, rΘΘcrire tous les liens en externe
  1645.             if ((nofollow) || (opt->state.stop))
  1646.               p_nocatch=1;
  1647.  
  1648.             // Θcrire codebase avant, flusher avant code
  1649.             if ((p_type==-1) || (p_type==-2)) {
  1650.               if ((opt->getmode & 1) && (ptr>0)) {
  1651.                 HT_ADD_ADR;    // refresh
  1652.               }
  1653.               lastsaved=adr;    // dernier Θcrit+1
  1654.             }
  1655.  
  1656.             // sauter espaces
  1657.             // adr+=p;
  1658.             INCREMENT_CURRENT_ADR(p);
  1659.             while( ( is_space(*adr) || (
  1660.               inscriptgen 
  1661.               && adr[0] == '\\' 
  1662.               && is_space(adr[1])
  1663.               )
  1664.               )
  1665.               && quote == '\0'
  1666.               ) {
  1667.                 if (!quote)
  1668.                   if ((*adr=='\"') || (*adr=='\'')) {
  1669.                     quote=*adr;                     // on doit attendre cela α la fin
  1670.                     if (inscriptgen && *(adr - 1) == '\\') {
  1671.                       quoteinscript=1;  /* will wait for \" */
  1672.                     }
  1673.                   }
  1674.                   // puis quitter
  1675.                   // adr++;    // sauter les espaces, "" et cie
  1676.                   INCREMENT_CURRENT_ADR(1);
  1677.               }
  1678.  
  1679.               /* Stop at \n (LF) if primary links or link lists */
  1680.               if (ptr == 0 || (in_media && strcmp(in_media,"LNK")==0))
  1681.                 quote='\n';
  1682.               /* s'arrΩter que ce soit un ' ou un " : pour document.write('<img src="foo'+a); par exemple! */
  1683.               else if (inscript && ! unquoted_script)
  1684.                 noquote=1;
  1685.  
  1686.               // sauter Θventuel \" ou \' javascript
  1687.               if (inscript) {    // on est dans un obj.write("..
  1688.                 if (*adr=='\\') {
  1689.                   if ((*(adr+1)=='\'') || (*(adr+1)=='"')) {  // \" ou \'
  1690.                     // adr+=2;    // sauter
  1691.                     INCREMENT_CURRENT_ADR(2);
  1692.                   }
  1693.                 }
  1694.               }
  1695.  
  1696.               // sauter content="1;URL=http://..
  1697.               if (p_searchMETAURL) {
  1698.                 int l=0;
  1699.                 while(
  1700.                   (adr + l + 4 < r->adr + r->size)
  1701.                   && (!strfield(adr+l,"URL=")) 
  1702.                   && (l<128) ) l++;
  1703.                 if (!strfield(adr+l,"URL="))
  1704.                   ok=-1;
  1705.                 else
  1706.                   adr+=(l+4);
  1707.               }
  1708.  
  1709.               /* Θviter les javascript:document.location=.. : les parser, plut⌠t */
  1710.               if (ok!=-1) {
  1711.                 if (strfield(adr,"javascript:") 
  1712.                   && ! inscript       /* we don't want to parse 'javascript:' inside document.write inside scripts */
  1713.                   ) {
  1714.                     ok=-1;
  1715.                     /*
  1716.                     On est dΘsormais dans du code javascript
  1717.                     */
  1718.                     inscript_name="";
  1719.                     inscript_tag=inscript=1;
  1720.                     inscript_state_pos=INSCRIPT_START;
  1721.                     inscript_tag_lastc=quote;     /* α attendre α la fin */
  1722.                     if (opt->parsedebug) { HT_ADD("<@@ inscript @@>"); }
  1723.                   }
  1724.               }
  1725.  
  1726.               if (p_type==1) {
  1727.                 if (*adr=='#') {
  1728.                   adr++;           // sauter # pour usemap etc
  1729.                 }
  1730.               }
  1731.               eadr=adr;
  1732.  
  1733.               // ne pas flusher aprΦs code si on doit Θcrire le codebase avant!
  1734.               if ((p_type!=-1) && (p_type!=2) && (p_type!=-2)) {
  1735.                 if ((opt->getmode & 1) && (ptr>0)) {
  1736.                   HT_ADD_ADR;    // refresh
  1737.                 }
  1738.                 lastsaved=adr;    // dernier Θcrit+1
  1739.                 // aprΦs on Θcrira soit les donnΘes initiales,
  1740.                 // soir une URL/lien modifiΘ!
  1741.               } else if (p_type==-1) p_flush=adr;    // flusher jusqu'α adr ensuite
  1742.  
  1743.               if (ok!=-1) {    // continuer
  1744.                 // dΘcouper le lien
  1745.                 do {
  1746.                   if ((* (unsigned char*) eadr)<32) {   // caractΦre de contr⌠le (ou \0)
  1747.                     if (!is_space(*eadr))
  1748.                       ok=0; 
  1749.                   }
  1750.                   if ( ( ((int) (eadr - adr)) ) > HTS_URLMAXSIZE)  // ** trop long, >HTS_URLMAXSIZE caractΦres (on prΘvoit HTS_URLMAXSIZE autres pour path)
  1751.                     ok=-1;    // ne pas traiter ce lien
  1752.  
  1753.                   if (ok > 0) {
  1754.                     //if (*eadr!=' ') {  
  1755.                     if (is_space(*eadr)) {   // guillemets,CR, etc
  1756.                       if ( 
  1757.                         ( *eadr == quote && ( !quoteinscript || *(eadr -1) == '\\') )  // end quote
  1758.                         || ( noquote && (*eadr == '\"' || *eadr == '\'') )       // end at any quote
  1759.                         || (!noquote && quote == '\0' && is_realspace(*eadr) )   // unquoted href
  1760.                         )     // si pas d'attente de quote spΘciale ou si quote atteinte
  1761.                         ok=0; 
  1762.                     } else if (ending_p && (*eadr==ending_p))
  1763.                       ok=0;
  1764.                     else {
  1765.                       switch(*eadr) {
  1766.                     case '>': 
  1767.                       if (!quote) {
  1768.                         if (!inscript && !in_media) {
  1769.                           intag=0;    // PLUS dans un tag!
  1770.                           intag_start_valid=0;
  1771.                                                     intag_name = NULL;
  1772.                         }
  1773.                         ok=0;
  1774.                       }
  1775.                       break;
  1776.                       /*case '<':*/ 
  1777.                     case '#': 
  1778.                       if (*(eadr-1) != '&')       // (
  1779.                         ok=0; 
  1780.                       break;
  1781.                       // case '?': non!
  1782.                     case '\\': if (inscript) ok=0; break;     // \" ou \' point d'arrΩt
  1783.                     case '?': quote_adr=adr; break;           // noter position query
  1784.                       }
  1785.                     }
  1786.                     //}
  1787.                   } 
  1788.                   eadr++;
  1789.                 } while(ok==1);
  1790.  
  1791.                 // Empty link detected
  1792.                 if ( (((int) (eadr - adr))) <= 1) {       // link empty
  1793.                   ok=-1;        // No
  1794.                   if (*adr != '#') {        // Not empty+unique #
  1795.                     if ( (((int) (eadr - adr)) == 1)) {       // 1=link empty with delim (end_adr-start_adr)
  1796.                       if (quote) {
  1797.                         if ((opt->getmode & 1) && (ptr>0)) { 
  1798.                           HT_ADD("#");        // We add this for a <href="">
  1799.                         }
  1800.                       }
  1801.                     }
  1802.                   }
  1803.                 }
  1804.  
  1805.                 // This is a dirty and horrible hack to avoid parsing an Adobe GoLive bogus tag
  1806.                 if (strfield(adr, "(Empty Reference!)")) {
  1807.                   ok=-1;        // No
  1808.                 }
  1809.  
  1810.               }
  1811.  
  1812.               if (ok==0) {    // tester un lien
  1813.                 char BIGSTK lien[HTS_URLMAXSIZE*2];
  1814.                 int meme_adresse=0;      // 0 par dΘfaut pour primary
  1815.                 //char *copie_de_adr=adr;
  1816.                 //char* p;
  1817.  
  1818.                 // construire lien (dΘcoupage)
  1819.                 if ( (((int) (eadr -  adr))-1) < HTS_URLMAXSIZE  ) {    // pas trop long?
  1820.                   strncpy(lien,adr,((int) (eadr - adr))-1);
  1821.                   *(lien+  (((int) (eadr -  adr)))-1  )='\0';
  1822.                   //printf("link: %s\n",lien);          
  1823.                   // supprimer les espaces
  1824.                   while((lien[strlen(lien)-1]==' ') && (strnotempty(lien))) lien[strlen(lien)-1]='\0';
  1825.  
  1826.  
  1827.                 } else
  1828.                   lien[0]='\0';    // erreur
  1829.  
  1830.  
  1831.                 // ------------------------------------------------------
  1832.                 // Lien repΘrΘ et extrait
  1833.                 if (strnotempty(lien)>0) {           // construction du lien
  1834.                   char BIGSTK adr[HTS_URLMAXSIZE*2],fil[HTS_URLMAXSIZE*2];          // ATTENTION adr cache le "vrai" adr
  1835.                   int forbidden_url=-1;              // lien non interdit (mais non autorisΘ..)
  1836.                   int just_test_it=0;                // mode de test des liens
  1837.                   int set_prio_to=0;                 // pour capture de page isolΘe
  1838.                   int import_done=0;                 // lien importΘ (ne pas scanner ensuite *α priori*)
  1839.                   //
  1840.                   adr[0]='\0'; fil[0]='\0';
  1841.                   //
  1842.                   // 0: autorisΘ
  1843.                   // 1: interdit (patcher tout de mΩme adresse)
  1844.  
  1845.                   if ((opt->debug>1) && (opt->log!=NULL)) {
  1846.                     HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link detected in html (tag): %s"LF,lien); test_flush;
  1847.                   }
  1848.  
  1849.                   // external check
  1850.                   if (!RUN_CALLBACK1(opt, linkdetected, lien) || !RUN_CALLBACK2(opt, linkdetected2, lien, intag_start)) {
  1851.                     error=1;    // erreur
  1852.                     if (opt->log) {
  1853.                       HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Link %s refused by external wrapper"LF,lien);
  1854.                       test_flush;
  1855.                     }
  1856.                   }
  1857.  
  1858. #if HTS_STRIP_DOUBLE_SLASH
  1859.                   // supprimer les // en / (sauf pour http://)
  1860.                   if (opt->urlhack) {
  1861.                     char *a,*p,*q;
  1862.                     int done=0;
  1863.                     a=strchr(lien,':');    // http://
  1864.                     if (a) {
  1865.                       a++;
  1866.                       while(*a=='/') a++;    // position aprΦs http://
  1867.                     } else {
  1868.                       a=lien;                // dΘbut
  1869.                       while(*a=='/') a++;    // position aprΦs http://
  1870.                     }
  1871.                     q=strchr(a,'?');     // ne pas traiter aprΦs '?'
  1872.                     if (!q)
  1873.                       q=a+strlen(a)-1;
  1874.                     while(( p=strstr(a,"//")) && (!done) ) {    // remplacer // par /
  1875.                       if ((int) p>(int) q) {   // aprΦs le ? (toto.cgi?param=1//2.3)
  1876.                         done=1;    // stopper
  1877.                       } else {
  1878.                         char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1879.                         tempo[0]='\0';
  1880.                         strncatbuff(tempo,a,(int) p - (int) a);
  1881.                         strcatbuff (tempo,p+1);
  1882.                         strcpybuff(a,tempo);    // recopier
  1883.                       }
  1884.                     }
  1885.                   }
  1886. #endif
  1887.  
  1888.                   // purger espaces de dΘbut et fin, CR,LF rΘsiduels
  1889.                   // (IMG SRC="foo.<\n><\t>gif<\t>")
  1890.                   {
  1891.                     char* a = lien;
  1892.                     size_t llen;
  1893.  
  1894.                     // strip ending spaces
  1895.                     llen = ( *a != '\0' ) ? strlen(a) : 0;
  1896.                     while(llen > 0 && is_realspace(lien[llen - 1]) ) {
  1897.                       a[--llen]='\0';
  1898.                     } 
  1899.                     //  skip leading ones
  1900.                     while(is_realspace(*a)) a++;
  1901.                     // strip cr, lf, tab inside URL
  1902.                     llen = 0;
  1903.                     while(*a) {
  1904.                       if (*a != '\n' && *a != '\r' && *a != '\t') {
  1905.                         lien[llen++] = *a;
  1906.                       }
  1907.                       a++;
  1908.                     }
  1909.                     lien[llen] = '\0';
  1910.                   }
  1911.  
  1912.                   // commas are forbidden
  1913.                   if (archivetag_p) {
  1914.                     if (strchr(lien, ',')) {
  1915.                       error=1;    // erreur
  1916.                       if ((opt->debug>1) && (opt->log!=NULL)) {
  1917.                         HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link rejected (multiple-archive) %s"LF,lien); test_flush;
  1918.                       }
  1919.                     }
  1920.                   }               
  1921.  
  1922.                   /* Unescape/escape %20 and other   */
  1923.                   {
  1924.                     char BIGSTK query[HTS_URLMAXSIZE*2];
  1925.                     char* a=strchr(lien,'?');
  1926.                     if (a) {
  1927.                       strcpybuff(query,a);
  1928.                       *a='\0';
  1929.                     } else
  1930.                       query[0]='\0';
  1931.                     // conversion & -> & et autres joyeusetΘs
  1932.                     unescape_amp(lien);
  1933.                     unescape_amp(query);
  1934.                     // dΘcoder l'inutile (%2E par exemple) et coder espaces
  1935.                     // XXXXXXXXXXXXXXXXX strcpybuff(lien,unescape_http(lien));
  1936.                     //strcpybuff(lien,unescape_http_unharm(lien, (no_esc_utf)?0:1));
  1937.                                         /* Never unescape high-chars (we don't know the encoding!!) */
  1938.                     strcpybuff(lien,unescape_http_unharm(catbuff,lien, 1));   /* note: '%' is still escaped */
  1939.                     escape_remove_control(lien);
  1940.                     escape_spc_url(lien);
  1941.                     strcatbuff(lien,query);     /* restore */
  1942.                   }
  1943.  
  1944.                   // convertir les Θventuels \ en des / pour Θviter des problΦmes de reconnaissance!
  1945.                   {
  1946.                     char* a;
  1947.                     for(a = jump_identification(lien) ; *a != '\0' && *a != '?' ; a++) {
  1948.                       if (*a == '\\') {
  1949.                         *a = '/';
  1950.                       }
  1951.                     }
  1952.                   }
  1953.  
  1954.                   // supprimer le(s) ./
  1955.                   while ((lien[0]=='.') && (lien[1]=='/')) {
  1956.                     char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1957.                     strcpybuff(tempo,lien+2);
  1958.                     strcpybuff(lien,tempo);
  1959.                   }
  1960.                   if (strnotempty(lien)==0)  // sauf si plus de nom de fichier
  1961.                     strcpybuff(lien,"./");
  1962.  
  1963.                   // vΘrifie les /~machin -> /~machin/
  1964.                   // supposition dangereuse?
  1965.                   // OUI!!
  1966. #if HTS_TILDE_SLASH
  1967.                   if (lien[strlen(lien)-1]!='/') {
  1968.                     char *a=lien+strlen(lien)-1;
  1969.                     // Θviter aussi index~1.html
  1970.                     while (((int) a>(int) lien) && (*a!='~') && (*a!='/') && (*a!='.')) a--;
  1971.                     if (*a=='~') {
  1972.                       strcatbuff(lien,"/");    // ajouter slash
  1973.                     }
  1974.                   }
  1975. #endif
  1976.  
  1977.                   // APPLET CODE="mixer.MixerApplet.class" --> APPLET CODE="mixer/MixerApplet.class"
  1978.                   // yes, this is dirty
  1979.                   // but I'm so lazzy..
  1980.                   // and besides the java "code" convention is really a pain in html code
  1981.                   if (p_type==-1) {
  1982.                     char* a=strrchr(lien,'.');
  1983.                     add_class_dots_to_patch=0;
  1984.                     if (a) {
  1985.                       char* b;
  1986.                       do {
  1987.                         b=strchr(lien,'.');
  1988.                         if ((b != a) && (b)) {
  1989.                           add_class_dots_to_patch++;
  1990.                           *b='/';
  1991.                         }
  1992.                       } while((b != a) && (b));
  1993.                     }
  1994.                   }
  1995.  
  1996.                   // Θliminer les Θventuels :80 (port par dΘfaut!)
  1997.                   if (link_has_authority(lien)) {
  1998.                     char * a;
  1999.                     a=strstr(lien,"//");    // "//" authority
  2000.                     if (a)
  2001.                       a+=2;
  2002.                     else
  2003.                       a=lien;
  2004.                     // while((*a) && (*a!='/') && (*a!=':')) a++;
  2005.                     a=jump_toport(a);
  2006.                     if (a) {  // port
  2007.                       int port=0;
  2008.                       int defport=80;
  2009.                       char* b=a+1;
  2010. #if HTS_USEOPENSSL
  2011.                       // FIXME
  2012.                       //if (strfield(adr, "https:")) {
  2013.                       //}
  2014. #endif
  2015.                       while(isdigit((unsigned char)*b)) { port*=10; port+=(int) (*b-'0'); b++; }
  2016.                       if (port==defport) {  // port 80, default - c'est dΘbile
  2017.                         char BIGSTK tempo[HTS_URLMAXSIZE*2];
  2018.                         tempo[0]='\0';
  2019.                         strncatbuff(tempo,lien,(int) (a - lien));
  2020.                         strcatbuff(tempo,a+3);  // sauter :80
  2021.                         strcpybuff(lien,tempo);
  2022.                       }
  2023.                     }
  2024.                   }
  2025.  
  2026.                   // filtrer les parazites (mailto & cie)
  2027.                   /*
  2028.                   if (strfield(lien,"mailto:")) {  // ne pas traiter
  2029.                   error=1;
  2030.                   } else if (strfield(lien,"news:")) {  // ne pas traiter
  2031.                   error=1;
  2032.                   }
  2033.                   */
  2034.  
  2035.                   // vΘrifier que l'on ne doit pas ajouter de .class
  2036.                   if (!error) {
  2037.                     if (add_class) {
  2038.                       char *a = lien+strlen(lien)-1;
  2039.                       while(( a > lien) && (*a!='/') && (*a!='.')) a--;
  2040.                       if (*a != '.')
  2041.                         strcatbuff(lien,".class");    // ajouter .class
  2042.                       else if (!strfield2(a,".class"))
  2043.                         strcatbuff(lien,".class");    // idem
  2044.                     }
  2045.                   }
  2046.  
  2047.                   // si c'est un chemin, alors vΘrifier (toto/toto.html -> http://www/toto/)
  2048.                   if (!error) {
  2049.                     if ((opt->debug>1) && (opt->log!=NULL)) {
  2050.                       HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"position link check %s"LF,lien); test_flush;
  2051.                     }
  2052.  
  2053.                     if ((p_type==2) || (p_type==-2)) {   // code ou codebase                        
  2054.                       // VΘrifier les codebase=applet (au lieu de applet/)
  2055.                       if (p_type==-2) {    // codebase
  2056.                         if (strnotempty(lien)) {
  2057.                           if (fil[strlen(lien)-1]!='/') {  // pas rΘpertoire
  2058.                             strcatbuff(lien,"/");
  2059.                           }
  2060.                         }
  2061.                       }
  2062.  
  2063.                       /* base has always authority */
  2064.                       if (p_type==2 && !link_has_authority(lien)) {
  2065.                         char BIGSTK tmp[HTS_URLMAXSIZE*2];
  2066.                         strcpybuff(tmp, "http://");
  2067.                         strcatbuff(tmp, lien);
  2068.                         strcpybuff(lien, tmp);
  2069.                       }
  2070.  
  2071.                       /* only one ending / (bug on some pages) */
  2072.                       if ((int)strlen(lien)>2) {
  2073.                         int len = (int) strlen(lien);
  2074.                         while(len > 1 && lien[len-1] == '/' && lien[len-2] == '/' )    /* double // (bug) */
  2075.                           lien[--len]='\0';
  2076.                       }
  2077.                       // copier nom host si besoin est
  2078.                       if (!link_has_authority(lien)) {  // pas de http://
  2079.                         char BIGSTK adr2[HTS_URLMAXSIZE*2],fil2[HTS_URLMAXSIZE*2];  // ** euh ident_url_relatif??
  2080.                         if (ident_url_relatif(lien,urladr,urlfil,adr2,fil2)<0) {                        
  2081.                           error=1;
  2082.                         } else {
  2083.                           strcpybuff(lien,"http://");
  2084.                           strcatbuff(lien,adr2);
  2085.                           if (*fil2!='/')
  2086.                             strcatbuff(lien,"/");
  2087.                           strcatbuff(lien,fil2);
  2088.                           {
  2089.                             char* a;
  2090.                             a=lien+strlen(lien)-1;
  2091.                             while((*a) && (*a!='/') && ( a> lien)) a--;
  2092.                             if (*a=='/') {
  2093.                               *(a+1)='\0';
  2094.                             }
  2095.                           }
  2096.                           //char BIGSTK tempo[HTS_URLMAXSIZE*2];
  2097.                           //strcpybuff(tempo,"http://");
  2098.                           //strcatbuff(tempo,urladr);    // host
  2099.                           //if (*lien!='/')
  2100.                           //  strcatbuff(tempo,"/");
  2101.                           //strcatbuff(tempo,lien);
  2102.                           //strcpybuff(lien,tempo);
  2103.                         }
  2104.                       }
  2105.  
  2106.                       if (!error) {  // pas d'erreur?
  2107.                         if (p_type==2) {   // code ET PAS codebase      
  2108.                           char* a=lien+strlen(lien)-1;
  2109.                           char* start_of_filename = jump_identification(lien);
  2110.                           if (start_of_filename != NULL 
  2111.                             && (start_of_filename = strchr(start_of_filename, '/')) != NULL)
  2112.                             start_of_filename++;
  2113.                           if (start_of_filename == NULL)
  2114.                             strcatbuff(lien, "/");
  2115.                           while( (a > lien) && (*a) && (*a!='/')) a--;
  2116.                           if (*a=='/') {     // ok on a repΘrΘ le dernier /
  2117.                             if (start_of_filename != NULL && a >= start_of_filename) {
  2118.                               *(a+1)='\0';   // couper
  2119.                             }
  2120.                           } else {
  2121.                             *lien='\0';    // Θliminer
  2122.                             error=1;   // erreur, ne pas poursuivre
  2123.                           }      
  2124.                         }
  2125.  
  2126.                         // stocker base ou codebase?
  2127.                         switch(p_type) {
  2128.                       case 2: { 
  2129.                         //if (*lien!='/') strcatbuff(base,"/");
  2130.                         strcpybuff(base,lien);
  2131.                               }
  2132.                               break;      // base
  2133.                       case -2: {
  2134.                         //if (*lien!='/') strcatbuff(codebase,"/");
  2135.                         strcpybuff(codebase,lien); 
  2136.                                }
  2137.                                break;  // base
  2138.                         }
  2139.  
  2140.                         if ((opt->debug>1) && (opt->log!=NULL)) {
  2141.                           HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"code/codebase link %s base %s"LF,lien,base); test_flush;
  2142.                         }
  2143.                         //printf("base code: %s - %s\n",lien,base);
  2144.                       }
  2145.  
  2146.                     } else {
  2147.                       char* _base;
  2148.                       if (p_type==-1)   // code (applet)
  2149.                         _base=codebase;
  2150.                       else
  2151.                         _base=base;
  2152.  
  2153.  
  2154.                       // ajouter chemin de base href..
  2155.                       if (strnotempty(_base)) {       // considΘrer base
  2156.                         if (!link_has_authority(lien)) {    // non absolue
  2157.                           if (*lien!='/') {           // non absolu sur le site (/)
  2158.                             if ( ((int) strlen(_base)+(int) strlen(lien))<HTS_URLMAXSIZE) {
  2159.                               // mailto: and co: do NOT add base
  2160.                               if (ident_url_relatif(lien,urladr,urlfil,adr,fil)>=0) {
  2161.                                 char BIGSTK tempo[HTS_URLMAXSIZE*2];
  2162.                                 // base est absolue
  2163.                                 strcpybuff(tempo,_base);
  2164.                                 strcatbuff(tempo,lien + ((*lien=='/')?1:0) );
  2165.                                 strcpybuff(lien,tempo);        // patcher en considΘrant base
  2166.                                 // ** vΘrifier que ../ fonctionne (ne doit pas arriver mais bon..)
  2167.  
  2168.                                 if ((opt->debug>1) && (opt->log!=NULL)) {
  2169.                                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link modified with code/codebase %s"LF,lien); test_flush;
  2170.                                 }
  2171.                               }
  2172.                             } else {
  2173.                               error=1;    // erreur
  2174.                               if (opt->log) {
  2175.                                 HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Link %s too long with base href"LF,lien);
  2176.                                 test_flush;
  2177.                               }
  2178.                             }
  2179.                           } else {
  2180.                             char BIGSTK badr[HTS_URLMAXSIZE*2], bfil[HTS_URLMAXSIZE*2];
  2181.                             if (ident_url_absolute(_base, badr, bfil) >=0 ) {
  2182.                               if ( ((int) strlen(badr)+(int) strlen(lien)) < HTS_URLMAXSIZE) {
  2183.                                 char BIGSTK tempo[HTS_URLMAXSIZE*2];
  2184.                                 // base est absolue
  2185.                                 tempo[0] = '\0';
  2186.                                 if (!link_has_authority(badr)) {
  2187.                                   strcatbuff(tempo, "http://");
  2188.                                 }
  2189.                                 strcatbuff(tempo,badr);
  2190.                                 strcatbuff(tempo,lien);
  2191.                                 strcpybuff(lien,tempo);        // patcher en considΘrant base
  2192.  
  2193.                                 if ((opt->debug>1) && (opt->log!=NULL)) {
  2194.                                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link modified with code/codebase %s"LF,lien); test_flush;
  2195.                                 }
  2196.                               } else {
  2197.                                 error=1;    // erreur
  2198.                                 if (opt->log) {
  2199.                                   HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Link %s too long with base href"LF,lien);
  2200.                                   test_flush;
  2201.                                 }
  2202.                               }
  2203.                             }
  2204.                           }
  2205.                         }
  2206.                       }
  2207.  
  2208.  
  2209.                     }
  2210.                   }
  2211.  
  2212.                   // transformer lien quelconque (http, relatif, etc) en une adresse
  2213.                   // et un chemin+fichier (adr,fil)
  2214.                   if (!error) {
  2215.                     int reponse;
  2216.                     if ((opt->debug>1) && (opt->log!=NULL)) {
  2217.                       HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"build relative link %s with %s%s"LF,lien,relativeurladr,relativeurlfil); test_flush;
  2218.                     }
  2219.                     if ((reponse=ident_url_relatif(lien,relativeurladr,relativeurlfil,adr,fil))<0) {                        
  2220.                       adr[0]='\0';    // erreur
  2221.                       if (reponse==-2) {
  2222.                         if (opt->log) {
  2223.                           HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Link %s not caught (unknown protocol)"LF,lien);
  2224.                           test_flush;
  2225.                         }
  2226.                       } else {
  2227.                         if ((opt->debug>1) && (opt->log!=NULL)) {
  2228.                           HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"ident_url_relatif failed for %s with %s%s"LF,lien,relativeurladr,relativeurlfil); test_flush;
  2229.                         }
  2230.                       }
  2231.                     } else {
  2232.                       if ((opt->debug>1) && (opt->log!=NULL)) {
  2233.                         HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"built relative link %s with %s%s -> %s%s"LF,lien,relativeurladr,relativeurlfil,adr,fil); test_flush;
  2234.                       }
  2235.                     }
  2236.                   } else {
  2237.                     if ((opt->debug>1) && (opt->log!=NULL)) {
  2238.                       HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link %s not build, error detected before"LF,lien); test_flush;
  2239.                     }
  2240.                     adr[0]='\0';
  2241.                   }
  2242.  
  2243. #if HTS_CHECK_STRANGEDIR
  2244.                   // !ATTENTION!
  2245.                   // Ici on teste les exotiques du genre www.truc.fr/machin (sans slash α la fin)
  2246.                   // je n'ai pas encore trouvΘ le moyen de faire la diffΘrence entre un rΘpertoire
  2247.                   // et un fichier en http A PRIORI : je fais donc un test
  2248.                   // En cas de moved xxx, on recalcule adr et fil, tout simplement
  2249.                   // DEFAUT: test effectuΘ plusieurs fois! α revoir!!!
  2250.                   if ((adr[0]!='\0') && (strcmp(adr,"file://") && (p_type!=2) && (p_type!=-2)) {
  2251.                     //## if ((adr[0]!='\0') && (adr[0]!=lOCAL_CHAR) && (p_type!=2) && (p_type!=-2)) {
  2252.                     if (fil[strlen(fil)-1]!='/') {  // pas rΘpertoire
  2253.                       if (ishtml(opt,fil)==-2) {    // pas d'extension
  2254.                         char BIGSTK loc[HTS_URLMAXSIZE*2];  // Θventuelle nouvelle position
  2255.                         loc[0]='\0';
  2256.                         if ((opt->debug>1) && (opt->log!=NULL)) {
  2257.                           HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link-check-directory: %s%s"LF,adr,fil);
  2258.                           test_flush;
  2259.                         }
  2260.  
  2261.                         // tester Θventuelle nouvelle position
  2262.                         switch (http_location(adr,fil,loc).statuscode) {
  2263.                       case 200: // ok au final
  2264.                         if (strnotempty(loc)) {  // a changΘ d'adresse
  2265.                           if (opt->log) {
  2266.                             HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Link %s%s has moved to %s for %s%s"LF,adr,fil,loc,urladr,urlfil);
  2267.                             test_flush;
  2268.                           }
  2269.  
  2270.                           // recalculer adr et fil!
  2271.                           if (ident_url_absolute(loc,adr,fil)==-1) {
  2272.                             adr[0]='\0';  // cancel
  2273.                             if ((opt->debug>1) && (opt->log!=NULL)) {
  2274.                               HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link-check-dir: %s%s"LF,adr,fil);
  2275.                               test_flush;
  2276.                             }
  2277.                           }
  2278.  
  2279.                         }
  2280.                         break;
  2281.                       case -2: case -3:  // timeout ou erreur grave
  2282.                         if (opt->log) {
  2283.                           HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Connection too slow for testing link %s%s (from %s%s)"LF,adr,fil,urladr,urlfil);
  2284.                           test_flush;
  2285.                         }
  2286.  
  2287.                         break;
  2288.                         }
  2289.  
  2290.                       }
  2291.                     } 
  2292.                   }
  2293. #endif
  2294.  
  2295.                   // Le lien doit juste Ωtre rΘΘcrit, mais ne doit pas gΘnΘrer un lien
  2296.                   // exemple: <FORM ACTION="url_cgi">
  2297.                   if (p_nocatch) {
  2298.                     forbidden_url=1;    // interdire rΘcupΘration du lien
  2299.                     if ((opt->debug>1) && (opt->log!=NULL)) {
  2300.                       HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link forced external at %s%s"LF,adr,fil);
  2301.                       test_flush;
  2302.                     }
  2303.                   }
  2304.  
  2305.                   // Tester si un lien doit Ωtre acceptΘ ou refusΘ (wizard)
  2306.                   // forbidden_url=1 : lien refusΘ
  2307.                   // forbidden_url=0 : lien acceptΘ
  2308.                   //if ((ptr>0) && (p_type!=2) && (p_type!=-2)) {    // tester autorisations?
  2309.                   if ((p_type!=2) && (p_type!=-2)) {    // tester autorisations?
  2310.                     if (!p_nocatch) {
  2311.                       if (adr[0]!='\0') {          
  2312.                         if ((opt->debug>1) && (opt->log!=NULL)) {
  2313.                           HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"wizard link test at %s%s.."LF,adr,fil);
  2314.                           test_flush;
  2315.                         }
  2316.                         forbidden_url=hts_acceptlink(opt,ptr,lien_tot,liens,
  2317.                           adr,fil,
  2318.                                                     intag_name ? intag_name : NULL, intag_name ? tag_attr_start : NULL,
  2319.                           &set_prio_to,
  2320.                           &just_test_it);
  2321.                         if ((opt->debug>1) && (opt->log!=NULL)) {
  2322.                           HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"result for wizard link test: %d"LF,forbidden_url);
  2323.                           test_flush;
  2324.                         }
  2325.                       }
  2326.                     }
  2327.                   }
  2328.  
  2329.                   // calculer meme_adresse
  2330.                   meme_adresse=strfield2(jump_identification(adr),jump_identification(urladr));
  2331.  
  2332.                   // DΘbut partie sauvegarde
  2333.  
  2334.                   // ici on forme le nom du fichier α sauver, et on patche l'URL
  2335.                   if (adr[0]!='\0') {
  2336.                     // savename: simplifier les ../ et autres joyeusetΘs
  2337.                     char BIGSTK save[HTS_URLMAXSIZE*2];
  2338.                     int r_sv=0;
  2339.                     // En cas de moved, adresse premiΦre
  2340.                     char BIGSTK former_adr[HTS_URLMAXSIZE*2];
  2341.                     char BIGSTK former_fil[HTS_URLMAXSIZE*2];
  2342.                     //
  2343.                     save[0]='\0'; former_adr[0]='\0'; former_fil[0]='\0';
  2344.                     //
  2345.  
  2346.                     // nom du chemin α sauver si on doit le calculer
  2347.                     // note: url_savename peut dΘcider de tester le lien si il le trouve
  2348.                     // suspect, et modifier alors adr et fil
  2349.                     // dans ce cas on aura une rΘfΘrence directe au lieu des traditionnels
  2350.                     // moved en cascade (impossible α reproduire α priori en local, lorsque des fichiers
  2351.                     // gif sont impliquΘs par exemple)
  2352.                     if ((p_type!=2) && (p_type!=-2)) {  // pas base href ou codebase
  2353.                       if (forbidden_url!=1) {
  2354.                         char BIGSTK last_adr[HTS_URLMAXSIZE*2];
  2355.                         last_adr[0]='\0';
  2356.                         //char last_fil[HTS_URLMAXSIZE*2]="";
  2357.                         strcpybuff(last_adr,adr);    // ancienne adresse
  2358.                         //strcpybuff(last_fil,fil);    // ancien chemin
  2359.                         r_sv=url_savename(adr,fil,save,former_adr,former_fil,liens[ptr]->adr,liens[ptr]->fil,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,NULL);
  2360.                         if (strcmp(jump_identification(last_adr),jump_identification(adr)) != 0) {  // a changΘ
  2361.  
  2362.                           // 2e test si moved
  2363.  
  2364.                           // Tester si un lien doit Ωtre acceptΘ ou refusΘ (wizard)
  2365.                           // forbidden_url=1 : lien refusΘ
  2366.                           // forbidden_url=0 : lien acceptΘ
  2367.                           if ((ptr>0) && (p_type!=2) && (p_type!=-2)) {    // tester autorisations?
  2368.                             if (!p_nocatch) {
  2369.                               if (adr[0]!='\0') {          
  2370.                                 if ((opt->debug>1) && (opt->log!=NULL)) {
  2371.                                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"wizard moved link retest at %s%s.."LF,adr,fil);
  2372.                                   test_flush;
  2373.                                 }
  2374.                                 forbidden_url=hts_acceptlink(opt,ptr,lien_tot,liens,
  2375.                                   adr,fil,
  2376.                                   intag_name ? intag_name : NULL, intag_name ? tag_attr_start : NULL,
  2377.                                   &set_prio_to,
  2378.                                   &just_test_it);
  2379.                                 if ((opt->debug>1) && (opt->log!=NULL)) {
  2380.                                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"result for wizard moved link retest: %d"LF,forbidden_url);
  2381.                                   test_flush;
  2382.                                 }
  2383.                               }
  2384.                             }
  2385.                           }
  2386.  
  2387.                           //import_done=1;    // c'est un import!
  2388.                           meme_adresse=0;   // on a changΘ
  2389.                         }
  2390.                       } else {
  2391.                         strcpybuff(save,"");  // dummy
  2392.                       }
  2393.                     }
  2394.  
  2395.                     // resolve unresolved type
  2396.                     if (r_sv!=-1
  2397.                       && p_type != 2 && p_type != -2
  2398.                       && forbidden_url == 0
  2399.                       && IS_DELAYED_EXT(save)
  2400.                       )
  2401.                     {  // pas d'erreur, on continue
  2402.                       r_sv = hts_wait_delayed(str, adr, fil, save, parenturladr, parenturlfil, former_adr, former_fil, &forbidden_url);
  2403.                     }
  2404.  
  2405.                     // record!
  2406.                     if (r_sv!=-1) {  // pas d'erreur, on continue
  2407.                       /* log */
  2408.                       if ((opt->debug>1) && (opt->log!=NULL)) {
  2409.                         HTS_LOG(opt,LOG_DEBUG);
  2410.                         if (forbidden_url!=1) {    // le lien va Ωtre chargΘ
  2411.                           if ((p_type==2) || (p_type==-2)) {  // base href ou codebase, pas un lien
  2412.                             fprintf(opt->log,"Code/Codebase: %s%s"LF,adr,fil);
  2413.                           } else if ((opt->getmode & 4)==0) {
  2414.                             fprintf(opt->log,"Record: %s%s -> %s"LF,adr,fil,save);
  2415.                           } else {
  2416.                             if (!ishtml(opt,fil))
  2417.                               fprintf(opt->log,"Record after: %s%s -> %s"LF,adr,fil,save);
  2418.                             else
  2419.                               fprintf(opt->log,"Record: %s%s -> %s"LF,adr,fil,save);
  2420.                           } 
  2421.                         } else
  2422.                           fprintf(opt->log,"External: %s%s"LF,adr,fil);
  2423.                         test_flush;
  2424.                       }
  2425.                       /* FIN log */
  2426.  
  2427.                       // Θcrire lien
  2428.                       if ((p_type==2) || (p_type==-2)) {  // base href ou codebase, sauter
  2429.                         lastsaved=eadr-1+1;  // sauter "
  2430.                       }
  2431.                       /* */
  2432.                       else if (opt->urlmode==0) {    // URL absolue dans tous les cas
  2433.                         if ((opt->getmode & 1) && (ptr>0)) {    // ecrire les html
  2434.                           if (!link_has_authority(adr)) {
  2435.                             HT_ADD("http://");
  2436.                           } else {
  2437.                             char* aut = strstr(adr, "//");
  2438.                             if (aut) {
  2439.                               char tmp[256];
  2440.                               tmp[0]='\0';
  2441.                               strncatbuff(tmp, adr, (int) (aut - adr));   // scheme
  2442.                               HT_ADD(tmp);          // Protocol
  2443.                               HT_ADD("//");
  2444.                             }
  2445.                           }
  2446.  
  2447.                           if (!opt->passprivacy) {
  2448.                             HT_ADD_HTMLESCAPED(jump_protocol(adr));           // Password
  2449.                           } else {
  2450.                             HT_ADD_HTMLESCAPED(jump_identification(adr));     // No Password
  2451.                           }
  2452.                           if (*fil!='/')
  2453.                             HT_ADD("/");
  2454.                           HT_ADD_HTMLESCAPED(fil);
  2455.                         }
  2456.                         lastsaved=eadr-1;    // dernier Θcrit+1 (enfin euh apres on fait un ++ alors hein)
  2457.                         /* */
  2458.                       } else if (opt->urlmode >= 4) {    // ne rien faire dans tous les cas!
  2459.                         /* */
  2460.                         /* leave the link 'as is' */
  2461.                         /* Sinon, dΘpend de interne/externe */
  2462.                       } else if (forbidden_url==1) {    // le lien ne sera pas chargΘ, rΘfΘrence externe!
  2463.                         if ((opt->getmode & 1) && (ptr>0)) {
  2464.                           if (p_type!=-1) {     // pas que le nom de fichier (pas classe java)
  2465.                             if (!opt->external) {
  2466.                               if (!link_has_authority(adr)) {
  2467.                                 HT_ADD("http://");
  2468.                                 if (!opt->passprivacy) {
  2469.                                   HT_ADD_HTMLESCAPED(adr);     // Password
  2470.                                 } else {
  2471.                                   HT_ADD_HTMLESCAPED(jump_identification(adr));     // No Password
  2472.                                 }
  2473.                                 if (*fil!='/')
  2474.                                   HT_ADD("/");
  2475.                                 HT_ADD_HTMLESCAPED(fil);
  2476.                               } else {
  2477.                                 char* aut = strstr(adr, "//");
  2478.                                 if (aut) {
  2479.                                   char tmp[256];
  2480.                                   tmp[0]='\0';
  2481.                                   strncatbuff(tmp, adr, (int) (aut - adr));   // scheme
  2482.                                   HT_ADD(tmp);          // Protocol
  2483.                                   HT_ADD("//");
  2484.                                   if (!opt->passprivacy) {
  2485.                                     HT_ADD_HTMLESCAPED(jump_protocol(adr));          // Password
  2486.                                   } else {
  2487.                                     HT_ADD_HTMLESCAPED(jump_identification(adr));     // No Password
  2488.                                   }
  2489.                                   if (*fil!='/')
  2490.                                     HT_ADD("/");
  2491.                                   HT_ADD_HTMLESCAPED(fil);
  2492.                                 }
  2493.                               }
  2494.                               //
  2495.                             } else {    // fichier/page externe, mais on veut gΘnΘrer une erreur
  2496.                               //
  2497.                               int patch_it=0;
  2498.                               int add_url=0;
  2499.                               char* cat_name=NULL;
  2500.                               char* cat_data=NULL;
  2501.                               int cat_nb=0;
  2502.                               int cat_data_len=0;
  2503.  
  2504.                               // ajouter lien external
  2505.                               switch ( (link_has_authority(adr)) ? 1 : ( (fil[strlen(fil)-1]=='/')?1:(ishtml(opt,fil))  ) ) {
  2506.                             case 1: case -2:       // html ou rΘpertoire
  2507.                               if (opt->getmode & 1) {  // sauver html
  2508.                                 patch_it=1;   // redirect
  2509.                                 add_url=1;    // avec link?
  2510.                                 cat_name="external.html";
  2511.                                 cat_nb=0;
  2512.                                 cat_data=HTS_DATA_UNKNOWN_HTML;
  2513.                                 cat_data_len=HTS_DATA_UNKNOWN_HTML_LEN;
  2514.                               }
  2515.                               break;
  2516.                             default:    // inconnu
  2517.                               // asp, cgi..
  2518.                               if ( (strfield2(fil+max(0,(int)strlen(fil)-4),".gif")) 
  2519.                                 || (strfield2(fil+max(0,(int)strlen(fil)-4),".jpg")) 
  2520.                                 || (strfield2(fil+max(0,(int)strlen(fil)-4),".xbm")) 
  2521.                                 /*|| (ishtml(opt,fil)!=0)*/ ) {
  2522.                                 patch_it=1;   // redirect
  2523.                               add_url=1;    // avec link aussi
  2524.                               cat_name="external.gif";
  2525.                               cat_nb=1;
  2526.                               cat_data=HTS_DATA_UNKNOWN_GIF;
  2527.                               cat_data_len=HTS_DATA_UNKNOWN_GIF_LEN;
  2528.                                 } else /* if (is_dyntype(get_ext(fil))) */ {
  2529.                                   patch_it=1;   // redirect
  2530.                                   add_url=1;    // avec link?
  2531.                                   cat_name="external.html";
  2532.                                   cat_nb=0;
  2533.                                   cat_data=HTS_DATA_UNKNOWN_HTML;
  2534.                                   cat_data_len=HTS_DATA_UNKNOWN_HTML_LEN;
  2535.                                 }
  2536.                                 break;
  2537.                               }// html,gif
  2538.  
  2539.                               if (patch_it) {
  2540.                                 char BIGSTK save[HTS_URLMAXSIZE*2];
  2541.                                 char BIGSTK tempo[HTS_URLMAXSIZE*2];
  2542.                                 strcpybuff(save,StringBuff(opt->path_html));
  2543.                                 strcatbuff(save,cat_name);
  2544.                                 if (lienrelatif(tempo,save, relativesavename)==0) {
  2545.                                                                     /* Never escape high-chars (we don't know the encoding!!) */
  2546.                                   escape_uri_utf(tempo);     // escape with %xx
  2547.                                   //if (!no_esc_utf)
  2548.                                   //  escape_uri(tempo);     // escape with %xx
  2549.                                   //else
  2550.                                   //  escape_uri_utf(tempo);     // escape with %xx
  2551.                                   HT_ADD_HTMLESCAPED(tempo);    // page externe
  2552.                                   if (add_url) {
  2553.                                     HT_ADD("?link=");    // page externe
  2554.  
  2555.                                     // same as above
  2556.                                     if (!link_has_authority(adr)) {
  2557.                                       HT_ADD("http://");
  2558.                                       if (!opt->passprivacy) {
  2559.                                         HT_ADD_HTMLESCAPED(adr);     // Password
  2560.                                       } else {
  2561.                                         HT_ADD_HTMLESCAPED(jump_identification(adr));     // No Password
  2562.                                       }
  2563.                                       if (*fil!='/')
  2564.                                         HT_ADD("/");
  2565.                                       HT_ADD_HTMLESCAPED(fil);
  2566.                                     } else {
  2567.                                       char* aut = strstr(adr, "//");
  2568.                                       if (aut) {
  2569.                                         char tmp[256];
  2570.                                         tmp[0]='\0';
  2571.                                         strncatbuff(tmp, adr, (int) (aut - adr) + 2);   // scheme
  2572.                                         HT_ADD(tmp);
  2573.                                         if (!opt->passprivacy) {
  2574.                                           HT_ADD_HTMLESCAPED(jump_protocol(adr));          // Password
  2575.                                         } else {
  2576.                                           HT_ADD_HTMLESCAPED(jump_identification(adr));     // No Password
  2577.                                         }
  2578.                                         if (*fil!='/')
  2579.                                           HT_ADD("/");
  2580.                                         HT_ADD_HTMLESCAPED(fil);
  2581.                                       }
  2582.                                     }
  2583.                                     //
  2584.  
  2585.                                   }
  2586.                                 }
  2587.  
  2588.                                 // Θcrire fichier?
  2589.                                 if (verif_external(opt,cat_nb,1)) {
  2590.                                   //if (!fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_html),cat_name))) {
  2591.                                   FILE* fp = filecreate(&opt->state.strc, fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_html),cat_name));
  2592.                                   if (fp) {
  2593.                                     if (cat_data_len==0) {   // texte
  2594.                                       verif_backblue(opt,StringBuff(opt->path_html));
  2595.                                       fprintf(fp,"%s%s","<!-- Created by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"LF,cat_data);
  2596.                                     } else {                    // data
  2597.                                       fwrite(cat_data,cat_data_len,1,fp);
  2598.                                     }
  2599.                                     fclose(fp);
  2600.                                     usercommand(opt,0,NULL,fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_html),cat_name),"","");
  2601.                                   }
  2602.                                 }
  2603.                               }  else {    // Θcrire normalement le nom de fichier
  2604.                                 HT_ADD("http://");
  2605.                                 if (!opt->passprivacy) {
  2606.                                   HT_ADD_HTMLESCAPED(adr);       // Password
  2607.                                 } else {
  2608.                                   HT_ADD_HTMLESCAPED(jump_identification(adr));       // No Password
  2609.                                 }
  2610.                                 if (*fil!='/')
  2611.                                   HT_ADD("/");
  2612.                                 HT_ADD_HTMLESCAPED(fil);
  2613.                               }// patcher?
  2614.                             }  // external
  2615.                           } else {  // que le nom de fichier (classe java)
  2616.                             // en gros recopie de plus bas: copier codebase et base
  2617.                             if (p_flush) {
  2618.                               char BIGSTK tempo[HTS_URLMAXSIZE*2];    // <-- ajoutΘ
  2619.                               char BIGSTK tempo_pat[HTS_URLMAXSIZE*2];
  2620.  
  2621.                               // Calculer chemin
  2622.                               tempo_pat[0]='\0';
  2623.                               strcpybuff(tempo,fil);  // <-- ajoutΘ
  2624.                               {
  2625.                                 char* a=strrchr(tempo,'/');
  2626.  
  2627.                                 // Example: we converted code="x.y.z.foo.class" into "x/y/z/foo.class"
  2628.                                 // we have to do the contrary now
  2629.                                 if (add_class_dots_to_patch>0) {
  2630.                                   while( (add_class_dots_to_patch>0) && (a) ) {
  2631.                                     *a='.';     // convert "false" java / into .
  2632.                                     add_class_dots_to_patch--;
  2633.                                     a=strrchr(tempo,'/');
  2634.                                   }
  2635.                                   // if add_class_dots_to_patch, this is because there is a problem!!
  2636.                                   if (add_class_dots_to_patch) {
  2637.                                     if (opt->log) {
  2638.                                       HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Error: can not rewind java path %s, check html code"LF,tempo);
  2639.                                       test_flush;
  2640.                                     }
  2641.                                   }
  2642.                                 }
  2643.  
  2644.                                 // Cut path/filename
  2645.                                 if (a) {
  2646.                                   char BIGSTK tempo2[HTS_URLMAXSIZE*2];
  2647.                                   strcpybuff(tempo2,a+1);         // FICHIER
  2648.                                   strncatbuff(tempo_pat,tempo,(int) (a - tempo)+1);  // chemin
  2649.                                   strcpybuff(tempo,tempo2);                     // fichier
  2650.                                 }
  2651.                               }
  2652.  
  2653.                               // Θrire codebase="chemin"
  2654.                               if ((opt->getmode & 1) && (ptr>0)) {
  2655.                                 char BIGSTK tempo4[HTS_URLMAXSIZE*2];
  2656.                                 tempo4[0]='\0';
  2657.  
  2658.                                 if (strnotempty(tempo_pat)) {
  2659.                                   HT_ADD("codebase=\"http://");
  2660.                                   if (!opt->passprivacy) {
  2661.                                     HT_ADD_HTMLESCAPED(adr);  // Password
  2662.                                   } else {
  2663.                                     HT_ADD_HTMLESCAPED(jump_identification(adr));  // No Password
  2664.                                   }
  2665.                                   if (*tempo_pat!='/') HT_ADD("/");
  2666.                                   HT_ADD(tempo_pat);
  2667.                                   HT_ADD("\" ");
  2668.                                 }
  2669.  
  2670.                                 strncatbuff(tempo4,lastsaved,(int) (p_flush - lastsaved));
  2671.                                 HT_ADD(tempo4);    // refresh code="
  2672.                                 HT_ADD(tempo);
  2673.                               }
  2674.                             }
  2675.                           }
  2676.                         }
  2677.                         lastsaved=eadr-1;
  2678.                       }
  2679.                       /*
  2680.                       else if (opt->urlmode==1) {    // ABSOLU, c'est le cas le moins courant
  2681.                       //  NE FONCTIONNE PAS!!  (et est inutile)
  2682.                       if ((opt->getmode & 1) && (ptr>0)) {    // ecrire les html
  2683.                       // Θcrire le lien modifiΘ, absolu
  2684.                       HT_ADD("file:");
  2685.                       if (*save=='/')
  2686.                       HT_ADD(save+1)
  2687.                       else
  2688.                       HT_ADD(save)
  2689.                       }
  2690.                       lastsaved=eadr-1;    // dernier Θcrit+1 (enfin euh apres on fait un ++ alors hein)
  2691.                       }
  2692.                       */
  2693.                       else if (opt->mimehtml) {
  2694.                         char BIGSTK buff[HTS_URLMAXSIZE*3];
  2695.                         HT_ADD("cid:");
  2696.                         strcpybuff(buff, adr);
  2697.                         strcatbuff(buff, fil);
  2698.                         escape_in_url(buff);
  2699.                         { char* a = buff; while((a = strchr(a, '%'))) { *a = 'X'; a++; } }
  2700.                         HT_ADD_HTMLESCAPED(buff);
  2701.                         lastsaved=eadr-1;    // dernier Θcrit+1 (enfin euh apres on fait un ++ alors hein)
  2702.                       }
  2703.                       else if (opt->urlmode==3) {    // URI absolue /
  2704.                         if ((opt->getmode & 1) && (ptr>0)) {    // ecrire les html
  2705.                           HT_ADD_HTMLESCAPED(fil);
  2706.                         }
  2707.                         lastsaved=eadr-1;    // dernier Θcrit+1 (enfin euh apres on fait un ++ alors hein)
  2708.                       }
  2709.                       else if (opt->urlmode==2) {  // RELATIF
  2710.                         char BIGSTK tempo[HTS_URLMAXSIZE*2];
  2711.                         tempo[0]='\0';
  2712.                         // calculer le lien relatif
  2713.  
  2714.                         if (lienrelatif(tempo,save,relativesavename)==0) {
  2715.                           if (!in_media) {    // In media (such as real audio): don't patch
  2716.                                                         /* Never escape high-chars (we don't know the encoding!!) */
  2717.                                                         escape_uri_utf(tempo);
  2718.                             //if (!no_esc_utf)
  2719.                             //  escape_uri(tempo);     // escape with %xx
  2720.                             //else {
  2721.                             //  /* No escaping at all - remaining upper chars will be escaped below */
  2722.                             //  /* FIXME - Should be done in all local cases */
  2723.                             //  //x_escape_html(tempo);
  2724.                             //  //escape_uri_utf(tempo);     // FIXME - escape with %xx
  2725.                             //  //escape_uri(tempo);     // escape with %xx
  2726.                             //}
  2727.                           }
  2728.                           if ((opt->debug>1) && (opt->log!=NULL)) {
  2729.                             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"relative link at %s build with %s and %s: %s"LF,adr,save,relativesavename,tempo);
  2730.                             test_flush;
  2731.                           }
  2732.  
  2733.                           // lien applet (code) - il faut placer un codebase avant
  2734.                           if (p_type==-1) {  // que le nom de fichier
  2735.  
  2736.                             if (p_flush) {
  2737.                               char BIGSTK tempo_pat[HTS_URLMAXSIZE*2];
  2738.                               tempo_pat[0]='\0';
  2739.                               {
  2740.                                 char* a=strrchr(tempo,'/');
  2741.  
  2742.                                 // Example: we converted code="x.y.z.foo.class" into "x/y/z/foo.class"
  2743.                                 // we have to do the contrary now
  2744.                                 if (add_class_dots_to_patch>0) {
  2745.                                   while( (add_class_dots_to_patch>0) && (a) ) {
  2746.                                     *a='.';     // convert "false" java / into .
  2747.                                     add_class_dots_to_patch--;
  2748.                                     a=strrchr(tempo,'/');
  2749.                                   }
  2750.                                   // if add_class_dots_to_patch, this is because there is a problem!!
  2751.                                   if (add_class_dots_to_patch) {
  2752.                                     if (opt->log) {
  2753.                                       HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Error: can not rewind java path %s, check html code"LF,tempo);
  2754.                                       test_flush;
  2755.                                     }
  2756.                                   }
  2757.                                 }
  2758.  
  2759.                                 if (a) {
  2760.                                   char BIGSTK tempo2[HTS_URLMAXSIZE*2];
  2761.                                   strcpybuff(tempo2,a+1);
  2762.                                   strncatbuff(tempo_pat,tempo,(int) (a - tempo)+1);  // chemin
  2763.                                   strcpybuff(tempo,tempo2);                     // fichier
  2764.                                 }
  2765.                               }
  2766.  
  2767.                               // Θrire codebase="chemin"
  2768.                               if ((opt->getmode & 1) && (ptr>0)) {
  2769.                                 char BIGSTK tempo4[HTS_URLMAXSIZE*2];
  2770.                                 tempo4[0]='\0';
  2771.  
  2772.                                 if (strnotempty(tempo_pat)) {
  2773.                                   HT_ADD("codebase=\"");
  2774.                                   HT_ADD_HTMLESCAPED(tempo_pat);
  2775.                                   HT_ADD("\" ");
  2776.                                 }
  2777.  
  2778.                                 strncatbuff(tempo4,lastsaved,(int) (p_flush - lastsaved));
  2779.                                 HT_ADD(tempo4);    // refresh code="
  2780.                               }
  2781.                             }
  2782.                             //lastsaved=adr;    // dernier Θcrit+1
  2783.                           }                              
  2784.  
  2785.                           if ((opt->getmode & 1) && (ptr>0)) {
  2786.                             // Θcrire le lien modifiΘ, relatif
  2787.                             // Note: escape all chars, even >127 (no UTF)
  2788.                             HT_ADD_HTMLESCAPED_FULL(tempo);
  2789.  
  2790.                             // Add query-string, for informational purpose only
  2791.                             // Useless, because all parameters-pages are saved into different targets
  2792.                             if (opt->includequery) {
  2793.                               char* a=strchr(lien,'?');
  2794.                               if (a) {
  2795.                                 HT_ADD_HTMLESCAPED(a);
  2796.                               }
  2797.                             }
  2798.                           }
  2799.                           lastsaved=eadr-1;    // dernier Θcrit+1 (enfin euh apres on fait un ++ alors hein)
  2800.                         } else {
  2801.                           if (opt->log) {
  2802.                             fprintf(opt->log,"Error building relative link %s and %s"LF,save,relativesavename);
  2803.                             test_flush;
  2804.                           }
  2805.                         }
  2806.                       }  // sinon le lien sera Θcrit normalement
  2807.  
  2808.  
  2809. #if 0
  2810.                       if (fexist(save)) {    // le fichier existe..
  2811.                         adr[0]='\0';
  2812.                         //if ((opt->debug>0) && (opt->log!=NULL)) {
  2813.                         if (opt->log) {
  2814.                           HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Link has already been written on disk, cancelled: %s"LF,save);
  2815.                           test_flush;
  2816.                         }
  2817.                       }
  2818. #endif                            
  2819.  
  2820.                       /* Security check */
  2821.                       if (strlen(save) >= HTS_URLMAXSIZE) {
  2822.                         adr[0]='\0';
  2823.                         if (opt->log) {
  2824.                           HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Link is too long: %s"LF,save);
  2825.                           test_flush;
  2826.                         }
  2827.                       }
  2828.  
  2829.                       if ((adr[0]!='\0') && (p_type!=2) && (p_type!=-2) && (forbidden_url!=1) ) {  // si le fichier n'existe pas, ajouter α la liste                            
  2830.                         // n'y a-t-il pas trop de liens?
  2831.                         if (lien_tot+1 >= lien_max-4) {    // trop de liens!
  2832.                           printf("PANIC! : Too many URLs : >%d [%d]\n",lien_tot,__LINE__);
  2833.                           if (opt->log) {
  2834.                             fprintf(opt->log,LF"Too many URLs, giving up..(>%d)"LF,lien_max);
  2835.                             fprintf(opt->log,"To avoid that: use #L option for more links (example: -#L1000000)"LF);
  2836.                             test_flush;
  2837.                           }
  2838.                           if ((opt->getmode & 1) && (ptr>0)) { if (fp) { fclose(fp); fp=NULL; } }
  2839.                           XH_uninit;   // dΘsallocation mΘmoire & buffers
  2840.                           return -1;
  2841.  
  2842.                         } else {    // noter le lien sur la listes des liens α charger
  2843.                           int pass_fix,dejafait=0;
  2844.  
  2845.                           // Calculer la prioritΘ de ce lien
  2846.                           if ((opt->getmode & 4)==0) {    // traiter html aprΦs
  2847.                             pass_fix=0;
  2848.                           } else {    // vΘrifier que ce n'est pas un !html
  2849.                             if (!ishtml(opt,fil))
  2850.                               pass_fix=1;        // prioritΘ infΘrieure (traiter aprΦs)
  2851.                             else
  2852.                               pass_fix=max(0,numero_passe);    // prioritΘ normale
  2853.                           }
  2854.  
  2855.                           /* If the file seems to be an html file, get depth-1 */
  2856.                           /*
  2857.                           if (strnotempty(save)) {
  2858.                           if (ishtml(opt,save) == 1) {
  2859.                           // descore_prio = 2;
  2860.                           } else {
  2861.                           // descore_prio = 1;
  2862.                           }
  2863.                           }
  2864.                           */
  2865.  
  2866.                           // vΘrifier que le lien n'a pas dΘja ΘtΘ notΘ
  2867.                           // si c'est le cas, alors il faut s'assurer que la prioritΘ associΘe
  2868.                           // au fichier est la plus grande des deux prioritΘs
  2869.                           //
  2870.                           // On part de la fin et on essaye de se presser (Θconomise temps machine)
  2871.                           {
  2872.                             int i=hash_read(hash,save,"",0,opt->urlhack);      // lecture type 0 (sav)
  2873.                             if (i>=0) {
  2874.                               if ((opt->debug>1) && (opt->log!=NULL)) {
  2875.                                 if (
  2876.                                   strcmp(adr, liens[i]->adr) != 0 
  2877.                                   || strcmp(fil, liens[i]->fil) != 0
  2878.                                   ) {
  2879.                                     HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"merging similar links %s%s and %s%s"LF,adr,fil,liens[i]->adr,liens[i]->fil);
  2880.                                     test_flush;
  2881.                                   }
  2882.                               }
  2883.                               liens[i]->depth=maximum(liens[i]->depth,liens[ptr]->depth - 1);
  2884.                               dejafait=1;
  2885.                             }
  2886.                           }
  2887.  
  2888.                           // le lien n'a jamais ΘtΘ crΘΘ.
  2889.                           // cette fois ci, on le crΘe!
  2890.                           if (!dejafait) {                                
  2891.                             //
  2892.                             // >>>> CREER LE LIEN <<<<
  2893.                             //
  2894.                             // enregistrer lien α charger
  2895.                             //liens[lien_tot]->adr[0]=liens[lien_tot]->fil[0]=liens[lien_tot]->sav[0]='\0';
  2896.                             // mΩme adresse: l'objet pΦre est l'objet pΦre de l'actuel
  2897.  
  2898.                                                         // DEBUT ROBOTS.TXT AJOUT
  2899.                                                         if (!just_test_it) {
  2900.                                                             if ((!strfield(adr,"ftp://"))         // non ftp
  2901.                                                                 && (!strfield(adr,"file://")) 
  2902. #if HTS_USEMMS
  2903.                                                                 && (!strfield(adr,"mms://")) 
  2904. #endif
  2905.                                                                 ) 
  2906.                                                             {    // non file
  2907.                                                                 if (opt->robots) {    // rΘcupΘrer robots
  2908.                                                                     if (ishtml(opt,fil)!=0) {                       // pas la peine pour des fichiers isolΘs
  2909.                                                                         if (checkrobots(_ROBOTS,adr,"") != -1) {    // robots.txt ?
  2910.                                                                             checkrobots_set(_ROBOTS ,adr,"");          // ajouter entrΘe vide
  2911.                                                                             if (checkrobots(_ROBOTS,adr,"") == -1) {    // robots.txt ?
  2912.                                                                                 // enregistrer robots.txt (MACRO)
  2913.                                                                                 liens_record(adr,"/robots.txt","","","");
  2914.                                                                                 if (liens[lien_tot]==NULL) {  // erreur, pas de place rΘservΘe
  2915.                                                                                     printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  2916.                                                                                     if (opt->log) { 
  2917.                                                                                         fprintf(opt->log,"Not enough memory, can not re-allocate %d bytes"LF,(int)((add_tab_alloc+1)*sizeof(lien_url)));
  2918.                                                                                         test_flush;
  2919.                                                                                     }
  2920.                                                                                     if ((opt->getmode & 1) && (ptr>0)) { if (fp) { fclose(fp); fp=NULL; } }
  2921.                                                                                     XH_uninit;    // dΘsallocation mΘmoire & buffers
  2922.                                                                                     return -1;
  2923.                                                                                 }  
  2924.                                                                                 liens[lien_tot]->testmode=0;          // pas mode test
  2925.                                                                                 liens[lien_tot]->link_import=0;       // pas mode import     
  2926.                                                                                 liens[lien_tot]->premier=lien_tot;
  2927.                                                                                 liens[lien_tot]->precedent=ptr;
  2928.                                                                                 liens[lien_tot]->depth=0;
  2929.                                                                                 liens[lien_tot]->pass2=max(0,numero_passe);
  2930.                                                                                 liens[lien_tot]->retry=0;
  2931.                                                                                 lien_tot++;  // UN LIEN DE PLUS
  2932. #if DEBUG_ROBOTS
  2933.                                                                                 printf("robots.txt: added file robots.txt for %s\n",adr);
  2934. #endif
  2935.                                                                                 if ((opt->debug>1) && (opt->log!=NULL)) {
  2936.                                                                                     HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"robots.txt added at %s"LF,adr);
  2937.                                                                                     test_flush;
  2938.                                                                                 }
  2939.                                                                             } else {
  2940.                                                                                 if (opt->log) {   
  2941.                                                                                     fprintf(opt->log,"Unexpected robots.txt error at %d"LF,__LINE__);
  2942.                                                                                     test_flush;
  2943.                                                                                 }
  2944.                                                                             }
  2945.                                                                         }
  2946.                                                                     }
  2947.                                                                 }
  2948.                                                             }
  2949.                                                         }
  2950.                                                         // FIN ROBOTS.TXT AJOUT
  2951.  
  2952.                                                         // enregistrer (MACRO)
  2953.                                                         liens_record(adr,fil,save,former_adr,former_fil);
  2954.                                                         if (liens[lien_tot]==NULL) {  // erreur, pas de place rΘservΘe
  2955.                                                             printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  2956.                                                             if (opt->log) { 
  2957.                                                                 fprintf(opt->log,"Not enough memory, can not re-allocate %d bytes"LF,(int)((add_tab_alloc+1)*sizeof(lien_url)));
  2958.                                                                 test_flush;
  2959.                                                             }
  2960.                               if ((opt->getmode & 1) && (ptr>0)) { if (fp) { fclose(fp); fp=NULL; } }
  2961.                               XH_uninit;    // dΘsallocation mΘmoire & buffers
  2962.                               return -1;
  2963.                             }  
  2964.  
  2965.                             // mode test?
  2966.                             if (!just_test_it)
  2967.                               liens[lien_tot]->testmode=0;          // pas mode test
  2968.                             else
  2969.                               liens[lien_tot]->testmode=1;          // mode test
  2970.                             if (!import_done)
  2971.                               liens[lien_tot]->link_import=0;       // pas mode import
  2972.                             else
  2973.                               liens[lien_tot]->link_import=1;       // mode import
  2974.                             // Θcrire autres paramΦtres de la structure-lien
  2975.                             if ((meme_adresse) && (!import_done) && (liens[ptr]->premier != 0))
  2976.                               liens[lien_tot]->premier=liens[ptr]->premier;
  2977.                             else    // sinon l'objet pΦre est le prΘcΘdent lui mΩme
  2978.                               liens[lien_tot]->premier=lien_tot;
  2979.                             // liens[lien_tot]->premier=ptr;
  2980.  
  2981.                             liens[lien_tot]->precedent=ptr;
  2982.                             // noter la prioritΘ
  2983.                             if (!set_prio_to)
  2984.                               liens[lien_tot]->depth=liens[ptr]->depth - 1;
  2985.                             else
  2986.                               liens[lien_tot]->depth=max(0,min(liens[ptr]->depth-1,set_prio_to-1));         // PRIORITE NULLE (catch page)
  2987.                             // noter pass
  2988.                             liens[lien_tot]->pass2=pass_fix;
  2989.                             liens[lien_tot]->retry=opt->retry;
  2990.  
  2991.                             //strcpybuff(liens[lien_tot]->adr,adr);
  2992.                             //strcpybuff(liens[lien_tot]->fil,fil);
  2993.                             //strcpybuff(liens[lien_tot]->sav,save); 
  2994.                             if ((opt->debug>1) && (opt->log!=NULL)) {
  2995.                               if (!just_test_it) {
  2996.                                 HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"OK, NOTE: %s%s -> %s"LF,liens[lien_tot]->adr,liens[lien_tot]->fil,liens[lien_tot]->sav);
  2997.                               } else {
  2998.                                 HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"OK, TEST: %s%s"LF,liens[lien_tot]->adr,liens[lien_tot]->fil);
  2999.                               }
  3000.                               test_flush;
  3001.                             }
  3002.  
  3003.                             lien_tot++;  // UN LIEN DE PLUS
  3004.                           } else { // if !dejafait
  3005.                             if ((opt->debug>1) && (opt->log!=NULL)) {
  3006.                               HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link has already been recorded, cancelled: %s"LF,save);
  3007.                               test_flush;
  3008.                             }
  3009.  
  3010.                           }
  3011.  
  3012.  
  3013.                         }   // si pas trop de liens
  3014.                       }   // si adr[0]!='\0'
  3015.  
  3016.  
  3017.                     }  // if adr[0]!='\0' 
  3018.  
  3019.                   }  // if adr[0]!='\0'
  3020.  
  3021.                 }    // if strlen(lien)>0
  3022.  
  3023.               }   // if ok==0      
  3024.  
  3025.               assertf(eadr - adr >= 0);       // Should not go back
  3026.               if (eadr > adr) {
  3027.                 INCREMENT_CURRENT_ADR(eadr - 1 - adr);
  3028.               }
  3029.               // adr=eadr-1;  // ** sauter
  3030.  
  3031.               /* We skipped bytes and skip the " : reset state */
  3032.               /*if (inscript) {
  3033.               inscript_state_pos = INSCRIPT_START;
  3034.               }*/
  3035.  
  3036.           }  // if (p) 
  3037.  
  3038.         }  // si '<' ou '>'
  3039.  
  3040.         // plus loin
  3041.         adr++;      // automate will be checked next loop
  3042.  
  3043.  
  3044.         /* Otimization: if we are scanning in HTML data (not in tag or script), 
  3045.         then jump to the next starting tag */
  3046.         if (ptr>0) {
  3047.           if ( (!intag)         /* Not in tag */
  3048.             && (!inscript)      /* Not in (java)script */
  3049.             && (!in_media)      /* Not in media */
  3050.             && (!incomment)     /* Not in comment (<!--) */
  3051.             && (!inscript_tag)  /* Not in tag with script inside */
  3052.             ) 
  3053.           {
  3054.             /* Not at the end */
  3055.             if (( ((int) (adr - r->adr)) ) < r->size) {
  3056.               /* Not on a starting tag yet */
  3057.               if (*adr != '<') {
  3058.                 /* strchr does not well behave with null chrs.. */
  3059.                 /* char* adr_next = strchr(adr,'<'); */
  3060.                 char* adr_next = adr;
  3061.                 while(*adr_next != '<' && (adr_next - r->adr) < r->size ) {
  3062.                   adr_next++;
  3063.                 }
  3064.                 /* Jump to near end (index hack) */
  3065.                 if (!adr_next || *adr_next != '<') {
  3066.                   if (
  3067.                     ( (int)(adr - r->adr) < (r->size - 4)) 
  3068.                     &&
  3069.                     (r->size > 4)
  3070.                     ) {
  3071.                       adr = r->adr + r->size - 2;
  3072.                     }
  3073.                 } else {
  3074.                   adr = adr_next;
  3075.                 }
  3076.               }
  3077.             }
  3078.           }
  3079.         }
  3080.  
  3081.         // ----------
  3082.         // Θcrire peu α peu
  3083.         if ((opt->getmode & 1) && (ptr>0)) HT_ADD_ADR;
  3084.         lastsaved=adr;    // dernier Θcrit+1
  3085.         // ----------
  3086.  
  3087.         // Checks
  3088.         if (back_add_stats != opt->state.back_add_stats) {
  3089.           back_add_stats = opt->state.back_add_stats;
  3090.  
  3091.           // Check max time
  3092.           if (!back_checkmirror(opt)) {
  3093.             adr = r->adr + r->size;
  3094.           }
  3095.         }
  3096.  
  3097.         // pour les stats du shell si parsing trop long
  3098.         if (r->size)
  3099.           opt->state._hts_in_html_done=(100 * ((int) (adr - r->adr)) ) / (int)(r->size);
  3100.         if (opt->state._hts_in_html_poll) {
  3101.           opt->state._hts_in_html_poll=0;
  3102.           // temps α attendre, et remplir autant que l'on peut le cache (backing)
  3103.           back_wait(sback,opt,cache,HTS_STAT.stat_timestart);        
  3104.           back_fillmax(sback,opt,cache,liens,ptr,numero_passe,lien_tot);
  3105.  
  3106.           // Transfer rate
  3107.           engine_stats();
  3108.  
  3109.           // Refresh various stats
  3110.           HTS_STAT.stat_nsocket=back_nsoc(sback);
  3111.           HTS_STAT.stat_errors=fspc(opt, NULL,"error");
  3112.           HTS_STAT.stat_warnings=fspc(opt, NULL,"warning");
  3113.           HTS_STAT.stat_infos=fspc(opt, NULL,"info");
  3114.           HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr);
  3115.           HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback);
  3116.  
  3117.           if (!RUN_CALLBACK7(opt, loop, sback->lnk, sback->count, 0,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)) {
  3118.             if (opt->log) {
  3119.               HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"Exit requested by shell or user"LF);
  3120.               test_flush;
  3121.             } 
  3122.             *stre->exit_xh_=1;  // exit requested
  3123.             XH_uninit;
  3124.             return -1;
  3125.             //adr = r->adr + r->size;  // exit
  3126.           } else if (opt->state._hts_cancel == 1) {
  3127.             // adr = r->adr + r->size;  // exit
  3128.             nofollow=1;               // moins violent
  3129.             opt->state._hts_cancel = 0;
  3130.           }
  3131.  
  3132.         }
  3133.  
  3134.         // refresh the backing system each 2 seconds
  3135.         if (engine_stats()) {
  3136.           back_wait(sback,opt,cache,HTS_STAT.stat_timestart);        
  3137.           back_fillmax(sback,opt,cache,liens,ptr,numero_passe,lien_tot);
  3138.         }
  3139.       } while(( ((int) (adr - r->adr)) ) < r->size);
  3140.  
  3141.             opt->state._hts_in_html_parsing=0;  // flag
  3142.       opt->state._hts_cancel=0;           // pas de cancel
  3143.  
  3144.             if ((opt->getmode & 1) && (ptr>0)) {
  3145.         {
  3146.           char* cAddr = ht_buff;
  3147.           int cSize = (int) ht_len;
  3148.           if ( (opt->debug>0) && (opt->log!=NULL) ) {
  3149.             HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: postprocess-html: %s%s"LF, urladr, urlfil);
  3150.           }
  3151.           if (RUN_CALLBACK4(opt, postprocess, &cAddr, &cSize, urladr, urlfil) == 1) {
  3152.             ht_buff = cAddr;
  3153.             ht_len = cSize;
  3154.           }
  3155.         }
  3156.  
  3157.         /* Flush and save to disk */
  3158.         HT_ADD_END;    // achever
  3159.       }
  3160.       //
  3161.       //
  3162.       //
  3163.     }  // if !error
  3164.  
  3165.  
  3166.     if (opt->getmode & 1) { if (fp) { fclose(fp); fp=NULL; } }
  3167.     // sauver fichier
  3168.     //structcheck(savename);
  3169.     //filesave(opt,r->adr,r->size,savename);
  3170.  
  3171.   }  // analyse OK
  3172.  
  3173.   /* Apply changes */
  3174.   ENGINE_SAVE_CONTEXT();
  3175.  
  3176.   return 0;
  3177. }
  3178.  
  3179.  
  3180.  
  3181.  
  3182. /*
  3183. Check 301, 302, .. statuscodes (moved)
  3184. */
  3185. int hts_mirror_check_moved(htsmoduleStruct* str, htsmoduleStructExtended* stre) {
  3186.   /* Load engine variables */
  3187.   ENGINE_LOAD_CONTEXT();  
  3188.  
  3189.   // DEBUT rattrapage des 301,302,307..
  3190.   // ------------------------------------------------------------
  3191.   if (!error) {
  3192.     ////////{
  3193.     // on a chargΘ un fichier en plus
  3194.     // if (!error) stat_loaded+=r.size;
  3195.  
  3196.     // ------------------------------------------------------------
  3197.     // Rattrapage des 301,302,307 (moved) et 412,416 - les 304 le sont dans le backing 
  3198.     // ------------------------------------------------------------
  3199.     if (HTTP_IS_REDIRECT(r->statuscode)) {          
  3200.         //if (r->adr!=NULL) {   // adr==null si fichier direct. [catch: davename normalement si cgi]
  3201.         //int i=0;
  3202.         char *rn=NULL;
  3203.         // char* p;
  3204.  
  3205.         if ( (opt->debug>0) && (opt->log!=NULL) ) {
  3206.           //if (opt->log) {
  3207.           HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"%s for %s%s"LF,r->msg,urladr,urlfil);
  3208.           test_flush;
  3209.         }
  3210.  
  3211.  
  3212.         {
  3213.           char BIGSTK mov_url[HTS_URLMAXSIZE*2],mov_adr[HTS_URLMAXSIZE*2],mov_fil[HTS_URLMAXSIZE*2];
  3214.           int get_it=0;         // ne pas prendre le fichier α la mΩme adresse par dΘfaut
  3215.           int reponse=0;
  3216.           mov_url[0]='\0'; mov_adr[0]='\0'; mov_fil[0]='\0';
  3217.           //
  3218.  
  3219.           strcpybuff(mov_url,r->location);
  3220.  
  3221.           // url qque -> adresse+fichier
  3222.           if ((reponse=ident_url_relatif(mov_url,urladr,urlfil,mov_adr,mov_fil))>=0) {                        
  3223.             int set_prio_to=0;    // pas de priotitΘ fixΘd par wizard
  3224.  
  3225.             // check whether URLHack is harmless or not
  3226.             if (opt->urlhack) {
  3227.               char BIGSTK n_adr[HTS_URLMAXSIZE*2], n_fil[HTS_URLMAXSIZE*2];
  3228.               char BIGSTK pn_adr[HTS_URLMAXSIZE*2], pn_fil[HTS_URLMAXSIZE*2];
  3229.               n_adr[0] = n_fil[0] = '\0';
  3230.               (void) adr_normalized(mov_adr, n_adr);
  3231.               (void) fil_normalized(mov_fil, n_fil);
  3232.               (void) adr_normalized(urladr, pn_adr);
  3233.               (void) fil_normalized(urlfil, pn_fil);
  3234.               if (strcasecmp(n_adr, pn_adr) == 0 && strcasecmp(n_fil, pn_fil) == 0) {
  3235.                 if (opt->log) {
  3236.                   HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Redirected link is identical because of 'URL Hack' option: %s%s and %s%s"LF, urladr, urlfil, mov_adr, mov_fil);
  3237.                   test_flush;
  3238.                 }
  3239.               }
  3240.             }
  3241.  
  3242.             //if (ident_url_absolute(mov_url,mov_adr,mov_fil)!=-1) {    // ok URL reconnue
  3243.             // c'est (en gros) la mΩme URL..
  3244.             // si c'est un problΦme de casse dans le host c'est que le serveur est buggΘ
  3245.             // ("RFC says.." : host name IS case insensitive)
  3246.             if ((strfield2(mov_adr,urladr)!=0) && (strfield2(mov_fil,urlfil)!=0)) {  // identique α casse prΦs
  3247.               // on tourne en rond
  3248.               if (strcmp(mov_fil,urlfil)==0) {
  3249.                 error=1;
  3250.                 get_it=-1;        // ne rien faire
  3251.                 if (opt->log) {
  3252.                   HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Can not bear crazy server (%s) for %s%s"LF,r->msg,urladr,urlfil);
  3253.                   test_flush;
  3254.                 }
  3255.               } else {    // mauvaise casse, effacer entrΘe dans la pile et rejouer une fois
  3256.                 get_it=1;
  3257.               }
  3258.             } else {        // adresse diffΘrente
  3259.               if (ishtml(opt,mov_url)==0) {   // pas mΩme adresse MAIS c'est un fichier non html (pas de page moved possible)
  3260.                 // -> on prend α cette adresse, le lien sera enregistrΘ avec lien_record() (hash)
  3261.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  3262.                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"wizard link test for moved file at %s%s.."LF,mov_adr,mov_fil);
  3263.                   test_flush;
  3264.                 }
  3265.                 // acceptΘ?
  3266.                 if (hts_acceptlink(opt,ptr,lien_tot,liens,
  3267.                   mov_adr,mov_fil,
  3268.                   NULL, NULL,
  3269.                   &set_prio_to,
  3270.                   NULL) != 1) {                /* nouvelle adresse non refusΘe ? */
  3271.                     get_it=1;
  3272.                     if ((opt->debug>1) && (opt->log!=NULL)) {
  3273.                       HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"moved link accepted: %s%s"LF,mov_adr,mov_fil);
  3274.                       test_flush;
  3275.                     }
  3276.                   }
  3277.               } /* sinon traitΘ normalement */
  3278.             }
  3279.  
  3280.             //if ((strfield2(mov_adr,urladr)!=0) && (strfield2(mov_fil,urlfil)!=0)) {  // identique α casse prΦs
  3281.             if (get_it==1) {
  3282.               // court-circuiter le reste du traitement
  3283.               // et reculer pour mieux sauter
  3284.               if (opt->log) {
  3285.                 HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Warning moved treated for %s%s (real one is %s%s)"LF,urladr,urlfil,mov_adr,mov_fil);
  3286.                 test_flush;
  3287.               }          
  3288.               // canceller lien actuel
  3289.               error=1;
  3290.               strcpybuff(liens[ptr]->adr,"!");  // caractΦre bidon (invalide hash)
  3291.               // noter NOUVEAU lien
  3292.               //xxc xxc
  3293.               //  set_prio_to=0+1;  // protection if the moved URL is an html page!!
  3294.               //xxc xxc
  3295.               {
  3296.                 char BIGSTK mov_sav[HTS_URLMAXSIZE*2];
  3297.                 // calculer lien et Θventuellement modifier addresse/fichier
  3298.                 if (url_savename(mov_adr,mov_fil,mov_sav,NULL,NULL,liens[liens[ptr]->precedent]->adr,liens[liens[ptr]->precedent]->fil,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,NULL)!=-1) { 
  3299.                   if (hash_read(hash,mov_sav,"",0,0)<0) {      // n'existe pas dΘja
  3300.                     // enregistrer lien (MACRO) avec SAV IDENTIQUE
  3301.                     liens_record(mov_adr,mov_fil,liens[ptr]->sav,"","");
  3302.                     //liens_record(mov_adr,mov_fil,mov_sav,"","");
  3303.                     if (liens[lien_tot]!=NULL) {    // OK, pas d'erreur
  3304.                       // mode test?
  3305.                       liens[lien_tot]->testmode=liens[ptr]->testmode;
  3306.                       liens[lien_tot]->link_import=0;       // mode normal
  3307.                       if (!set_prio_to)
  3308.                         liens[lien_tot]->depth=liens[ptr]->depth;
  3309.                       else
  3310.                         liens[lien_tot]->depth=max(0,min(set_prio_to-1,liens[ptr]->depth));       // PRIORITE NULLE (catch page)
  3311.                       liens[lien_tot]->pass2=max(liens[ptr]->pass2,numero_passe);
  3312.                       liens[lien_tot]->retry=liens[ptr]->retry;
  3313.                       liens[lien_tot]->premier=liens[ptr]->premier;
  3314.                       liens[lien_tot]->precedent=liens[ptr]->precedent;
  3315.                       lien_tot++;
  3316.                     } else {  // oups erreur, plus de mΘmoire!!
  3317.                       printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  3318.                       if (opt->log) {
  3319.                         fprintf(opt->log,"Not enough memory, can not re-allocate %d bytes"LF,(int)((add_tab_alloc+1)*sizeof(lien_url)));
  3320.                         test_flush;
  3321.                       }
  3322.                       //if (opt->getmode & 1) { if (fp) { fclose(fp); fp=NULL; } }
  3323.                       XH_uninit;    // dΘsallocation mΘmoire & buffers
  3324.                       return 0;
  3325.                     }
  3326.                   } else {
  3327.                     if ( (opt->debug>0) && (opt->log!=NULL) ) {
  3328.                       HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"moving %s to an existing file %s"LF,liens[ptr]->fil,urlfil);
  3329.                       test_flush;
  3330.                     }
  3331.                   }
  3332.  
  3333.                 }
  3334.               }
  3335.  
  3336.               //printf("-> %s %s %s\n",liens[lien_tot-1]->adr,liens[lien_tot-1]->fil,liens[lien_tot-1]->sav);
  3337.  
  3338.               // note mΘtaphysique: il se peut qu'il y ait un index.html et un INDEX.HTML
  3339.               // sous DOS ca marche pas trΦs bien... mais comme je suis gΘnial url_savename()
  3340.               // est α mΩme de rΘgler ce problΦme
  3341.             }
  3342.           } // ident_url_xx
  3343.  
  3344.           if (get_it==0) {    // adresse vraiment diffΘrente et potentiellement en html (pas de possibilitΘ de bouger la page tel quel α cause des <img src..> et cie)
  3345.             rn=(char*) calloct(8192,1);
  3346.             if (rn!=NULL) {
  3347.               if (opt->log) {
  3348.                 HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"File has moved from %s%s to %s"LF,urladr,urlfil,mov_url);
  3349.                 test_flush;
  3350.               }
  3351.               if (!opt->mimehtml) {
  3352.                 escape_uri(mov_url);
  3353.               } else {
  3354.                 char BIGSTK buff[HTS_URLMAXSIZE*3];
  3355.                 strcpybuff(buff, mov_adr);
  3356.                 strcatbuff(buff, mov_fil);
  3357.                 escape_in_url(buff);
  3358.                 { char* a = buff; while((a = strchr(a, '%'))) { *a = 'X'; a++; } }
  3359.                 strcpybuff(mov_url, "cid:");
  3360.                 strcatbuff(mov_url, buff);
  3361.               }
  3362.               // On prΘpare une page qui sautera immΘdiatement sur la bonne URL
  3363.               // Le scanner re-changera, ensuite, cette URL, pour la mirrorer!
  3364.               strcpybuff(rn,"<HTML>"CRLF);
  3365.               strcatbuff(rn,"<!-- Created by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"CRLF);
  3366.               strcatbuff(rn,"<HEAD>"CRLF"<TITLE>Page has moved</TITLE>"CRLF"</HEAD>"CRLF"<BODY>"CRLF);
  3367.               strcatbuff(rn,"<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=");
  3368.               strcatbuff(rn,mov_url);    // URL
  3369.               strcatbuff(rn,"\">"CRLF);
  3370.               strcatbuff(rn,"<A HREF=\"");
  3371.               strcatbuff(rn,mov_url);
  3372.               strcatbuff(rn,"\">");
  3373.               strcatbuff(rn,"<B>Click here...</B></A>"CRLF);
  3374.               strcatbuff(rn,"</BODY>"CRLF);
  3375.               strcatbuff(rn,"<!-- Created by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"CRLF);
  3376.               strcatbuff(rn,"</HTML>"CRLF);
  3377.  
  3378.               // changer la page
  3379.               if (r->adr) { 
  3380.                 freet(r->adr); 
  3381.                 r->adr=NULL; 
  3382.               }
  3383.               r->adr=rn;
  3384.               r->size=strlen(r->adr);
  3385.               strcpybuff(r->contenttype, "text/html");
  3386.             }
  3387.           }  // get_it==0
  3388.  
  3389.         }     // bloc
  3390.         // erreur HTTP (ex: 404, not found)
  3391.       } else if (
  3392.         (r->statuscode==412)
  3393.         || (r->statuscode==416)
  3394.         ) {    // Precondition Failed, c'est α dire pour nous redemander TOUT le fichier
  3395.           if (fexist(liens[ptr]->sav)) {
  3396.             remove(liens[ptr]->sav);    // Eliminer
  3397.             if (!fexist(liens[ptr]->sav)) {  // Bien ΘliminΘ? (sinon on boucle..)
  3398. #if HDEBUG
  3399.               printf("Partial content NOT up-to-date, reget all file for %s\n",liens[ptr]->sav);
  3400. #endif
  3401.               if ( (opt->debug>1) && (opt->log!=NULL) ) {
  3402.                 //if (opt->log) {
  3403.                 HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Partial file reget (%s) for %s%s"LF,r->msg,urladr,urlfil);
  3404.                 test_flush;
  3405.               }
  3406.               // enregistrer le MEME lien (MACRO)
  3407.               liens_record(liens[ptr]->adr,liens[ptr]->fil,liens[ptr]->sav,"","");
  3408.               if (liens[lien_tot]!=NULL) {    // OK, pas d'erreur
  3409.                 liens[lien_tot]->testmode=liens[ptr]->testmode;          // mode test?
  3410.                 liens[lien_tot]->link_import=0;       // pas mode import
  3411.                 liens[lien_tot]->depth=liens[ptr]->depth;
  3412.                 liens[lien_tot]->pass2=max(liens[ptr]->pass2,numero_passe);
  3413.                 liens[lien_tot]->retry=liens[ptr]->retry;
  3414.                 liens[lien_tot]->premier=liens[ptr]->premier;
  3415.                 liens[lien_tot]->precedent=ptr;
  3416.                 lien_tot++;
  3417.                 //
  3418.                 // canceller lien actuel
  3419.                 error=1;
  3420.                 strcpybuff(liens[ptr]->adr,"!");  // caractΦre bidon (invalide hash)
  3421.                 //
  3422.               } else {  // oups erreur, plus de mΘmoire!!
  3423.                 printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  3424.                 if (opt->log) {
  3425.                   fprintf(opt->log,"Not enough memory, can not re-allocate %d bytes"LF,(int)((add_tab_alloc+1)*sizeof(lien_url)));
  3426.                   test_flush;
  3427.                 }
  3428.                 //if (opt->getmode & 1) { if (fp) { fclose(fp); fp=NULL; } }
  3429.                 XH_uninit;    // dΘsallocation mΘmoire & buffers
  3430.                 return 0;
  3431.               } 
  3432.             } else {
  3433.               if (opt->log!=NULL) {
  3434.                 HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Can not remove old file %s"LF,urlfil);
  3435.                 test_flush;
  3436.               }
  3437.             }
  3438.           } else {
  3439.             if (opt->log!=NULL) {
  3440.               HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Unexpected 412/416 error (%s) for %s%s"LF,r->msg,urladr,urlfil);
  3441.               test_flush;
  3442.             }
  3443.           }
  3444.         } else if (r->statuscode!=HTTP_OK) {
  3445.           int can_retry=0;
  3446.  
  3447.           // cas o∙ l'on peut reessayer
  3448.           switch(r->statuscode) {
  3449.             //case -1: can_retry=1; break;
  3450.           case STATUSCODE_TIMEOUT:
  3451.             if (opt->hostcontrol) {    // timeout et retry ΘpuisΘs
  3452.               if ((opt->hostcontrol & 1) && (liens[ptr]->retry<=0)) {
  3453.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  3454.                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Link banned: %s%s"LF,urladr,urlfil); test_flush;
  3455.                 }
  3456.                 host_ban(opt,liens,ptr,lien_tot,sback,jump_identification(urladr));
  3457.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  3458.                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Info: previous log - link banned: %s%s"LF,urladr,urlfil); test_flush;
  3459.                 }
  3460.               } else can_retry=1;
  3461.             } else can_retry=1;
  3462.             break;
  3463.           case STATUSCODE_SLOW:
  3464.             if ((opt->hostcontrol) && (liens[ptr]->retry<=0)) {    // too slow
  3465.               if (opt->hostcontrol & 2) {
  3466.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  3467.                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Link banned: %s%s"LF,urladr,urlfil); test_flush;
  3468.                 }
  3469.                 host_ban(opt,liens,ptr,lien_tot,sback,jump_identification(urladr));
  3470.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  3471.                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Info: previous log - link banned: %s%s"LF,urladr,urlfil); test_flush;
  3472.                 }
  3473.               } else can_retry=1;
  3474.             } else can_retry=1;
  3475.             break;
  3476.           case STATUSCODE_CONNERROR:            // connect closed
  3477.             can_retry=1;
  3478.             break;
  3479.           case STATUSCODE_NON_FATAL:            // other (non fatal) error
  3480.             can_retry=1;
  3481.             break;
  3482.           case STATUSCODE_SSL_HANDSHAKE:            // bad SSL handskake
  3483.             can_retry=1;
  3484.             break;
  3485.           case 408: case 409: case 500: case 502: case 504: 
  3486.             can_retry=1;
  3487.             break;
  3488.           }
  3489.  
  3490.           if ( strcmp(liens[ptr]->fil,"/primary") != 0 ) {  // no primary (internal page 0)
  3491.             if ((liens[ptr]->retry<=0) || (!can_retry) ) {  // retry ΘpuisΘs (ou retry impossible)
  3492.               if (opt->log) {
  3493.                 if ((opt->retry>0) && (can_retry)){
  3494.                   HTS_LOG(opt,LOG_ERROR); 
  3495.                   fprintf(opt->log,"\"%s\" (%d) after %d retries at link %s%s (from %s%s)"LF,r->msg,r->statuscode,opt->retry,urladr,urlfil,liens[liens[ptr]->precedent]->adr,liens[liens[ptr]->precedent]->fil);
  3496.                 } else {
  3497.                   if (r->statuscode==STATUSCODE_TEST_OK) {    // test OK
  3498.                     if ((opt->debug>0) && (opt->log!=NULL)) {
  3499.                       HTS_LOG(opt,LOG_INFO); 
  3500.                       fprintf(opt->log,"Test OK at link %s%s (from %s%s)"LF,urladr,urlfil,liens[liens[ptr]->precedent]->adr,liens[liens[ptr]->precedent]->fil);
  3501.                     }
  3502.                   } else {
  3503.                     if (strcmp(urlfil,"/robots.txt")) {       // ne pas afficher d'infos sur robots.txt par dΘfaut
  3504.                       HTS_LOG(opt,LOG_ERROR); 
  3505.                       fprintf(opt->log,"\"%s\" (%d) at link %s%s (from %s%s)"LF,r->msg,r->statuscode,urladr,urlfil,liens[liens[ptr]->precedent]->adr,liens[liens[ptr]->precedent]->fil);
  3506.                     } else {
  3507.                       if (opt->debug>1) {
  3508.                         HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"No robots.txt rules at %s"LF,urladr);
  3509.                         test_flush;
  3510.                       }
  3511.                     }
  3512.                   }
  3513.                 }
  3514.                 test_flush;
  3515.               }
  3516.  
  3517.               // NO error in trop level
  3518.               // due to the "no connection -> previous restored" hack
  3519.               // This prevent the engine from wiping all data if the website has been deleted (or moved)
  3520.               // since last time (which is quite annoying)
  3521.               if (liens[ptr]->precedent != 0) {
  3522.                 // ici on teste si on doit enregistrer la page tout de mΩme
  3523.                 if (opt->errpage) {
  3524.                   store_errpage=1;
  3525.                 }
  3526.               } else {
  3527.                 if (strcmp(urlfil,"/robots.txt") != 0) {
  3528.                   /*
  3529.                   This is an error caused by a link entered by the user
  3530.                   That is, link(s) entered by user are invalid (404, 500, connect error, proxy error->.)
  3531.                   If all links entered are invalid, the session failed and we will attempt to restore
  3532.                   the previous one
  3533.                   Example: Try to update a website which has been deleted remotely: this may delete
  3534.                   the website locally, which is really not desired (especially if the website disappeared!)
  3535.                   With this hack, the engine won't wipe local files (how clever)
  3536.                   */
  3537.                   HTS_STAT.stat_errors_front++;
  3538.                 }
  3539.               }
  3540.  
  3541.             } else {    // retry!!
  3542.               if (opt->debug>0 && opt->log != NULL) {  // on fera un alert si le retry Θchoue               
  3543.                 HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Retry after error %d (%s) at link %s%s (from %s%s)"LF,r->statuscode,r->msg,urladr,urlfil,liens[liens[ptr]->precedent]->adr,liens[liens[ptr]->precedent]->fil);
  3544.                 test_flush;
  3545.               }
  3546.               // redemander fichier
  3547.               liens_record(urladr,urlfil,savename,"","");
  3548.               if (liens[lien_tot]!=NULL) {    // OK, pas d'erreur
  3549.                 liens[lien_tot]->testmode=liens[ptr]->testmode;          // mode test?
  3550.                 liens[lien_tot]->link_import=0;       // pas mode import
  3551.                 liens[lien_tot]->depth=liens[ptr]->depth;
  3552.                 liens[lien_tot]->pass2=max(liens[ptr]->pass2,numero_passe);
  3553.                 liens[lien_tot]->retry=liens[ptr]->retry-1;    // moins 1 retry!
  3554.                 liens[lien_tot]->premier=liens[ptr]->premier;
  3555.                 liens[lien_tot]->precedent=liens[ptr]->precedent;
  3556.                 lien_tot++;
  3557.               } else {  // oups erreur, plus de mΘmoire!!
  3558.                 printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  3559.                 if (opt->log) {
  3560.                   HTS_LOG(opt,LOG_PANIC); 
  3561.                   fprintf(opt->log,"Not enough memory, can not re-allocate %d bytes"LF,(int)((add_tab_alloc+1)*sizeof(lien_url)));
  3562.                   test_flush;
  3563.                 }
  3564.                 //if (opt->getmode & 1) { if (fp) { fclose(fp); fp=NULL; } }
  3565.                 XH_uninit;    // dΘsallocation mΘmoire & buffers
  3566.                 return 0;
  3567.               } 
  3568.             }
  3569.           } else {
  3570.             if (opt->log) {
  3571.               if (opt->debug>1) {
  3572.                 HTS_LOG(opt,LOG_INFO); 
  3573.                 fprintf(opt->log,"Info: no robots.txt at %s%s"LF,urladr,urlfil);
  3574.               }
  3575.             }
  3576.           }
  3577.           if (!store_errpage) {
  3578.             if (r->adr) {     // dΘsalloc
  3579.               freet(r->adr); 
  3580.               r->adr=NULL; 
  3581.             }
  3582.             error=1;  // erreur!
  3583.           }
  3584.         }
  3585.         // FIN rattrapage des 301,302,307..
  3586.         // ------------------------------------------------------------
  3587.  
  3588.   }  // if !error
  3589.  
  3590.  
  3591.   /* Apply changes */
  3592.   ENGINE_SAVE_CONTEXT();
  3593.  
  3594.   return 0;
  3595.  
  3596.  
  3597. }
  3598.  
  3599.  
  3600.  
  3601. /*
  3602. Wait for next file and
  3603. check 301, 302, .. statuscodes (moved)
  3604. */
  3605. int hts_mirror_wait_for_next_file(htsmoduleStruct* str, htsmoduleStructExtended* stre) {
  3606.   /* Load engine variables */
  3607.   ENGINE_LOAD_CONTEXT();
  3608.   /* */
  3609.   int b;
  3610.   int n;
  3611.  
  3612. #if BDEBUG==1
  3613.   printf("\nBack test..\n");
  3614. #endif
  3615.  
  3616.   // pause/lock files
  3617.   {
  3618.     int do_pause=0;
  3619.  
  3620.     // user pause lockfile : create hts-paused.lock --> HTTrack will be paused
  3621.     if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-stop.lock"))) {
  3622.       // remove lockfile
  3623.       remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-stop.lock"));
  3624.       if (!fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-stop.lock"))) {
  3625.         do_pause=1;
  3626.       }
  3627.     }
  3628.  
  3629.     // after receving N bytes, pause
  3630.     if (opt->fragment>0) {
  3631.       if ((HTS_STAT.stat_bytes-stat_fragment) > opt->fragment) {
  3632.         do_pause=1;
  3633.       }
  3634.     }
  3635.  
  3636.     // pause?
  3637.     if (do_pause) {
  3638.       if ( (opt->debug>0) && (opt->log!=NULL) ) {
  3639.         HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: pause requested.."LF);
  3640.       }
  3641.       while (back_nsoc(sback)>0) {                  // attendre fin des transferts
  3642.         back_wait(sback,opt,cache,HTS_STAT.stat_timestart);
  3643.         Sleep(200);
  3644.         {
  3645.           back_wait(sback,opt,cache,HTS_STAT.stat_timestart);
  3646.  
  3647.           // Transfer rate
  3648.           engine_stats();
  3649.  
  3650.           // Refresh various stats
  3651.           HTS_STAT.stat_nsocket=back_nsoc(sback);
  3652.           HTS_STAT.stat_errors=fspc(opt,NULL,"error");
  3653.           HTS_STAT.stat_warnings=fspc(opt,NULL,"warning");
  3654.           HTS_STAT.stat_infos=fspc(opt,NULL,"info");
  3655.           HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr);
  3656.           HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback);
  3657.  
  3658.           b=0;
  3659.           if (!RUN_CALLBACK7(opt, loop, sback->lnk, sback->count, b,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)
  3660.             || !back_checkmirror(opt)) {
  3661.               if (opt->log) {
  3662.                 HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"Exit requested by shell or user"LF);
  3663.                 test_flush;
  3664.               }
  3665.               *stre->exit_xh_=1;  // exit requested
  3666.               XH_uninit;
  3667.               return 0;
  3668.             }
  3669.         }
  3670.       }
  3671.       // On dΘsalloue le buffer d'enregistrement des chemins crΘΘe, au cas o∙ pendant la pause
  3672.       // l'utilisateur ferait un rm -r aprΦs avoir effectuΘ un tar
  3673.       // structcheck_init(1);
  3674.       {
  3675.         FILE* fp = fopen(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-paused.lock"),"wb");
  3676.         if (fp) {
  3677.           fspc(NULL,fp,"info");  // dater
  3678.           fprintf(fp,"Pause"LF"HTTrack is paused after retreiving "LLintP" bytes"LF"Delete this file to continue the mirror->.."LF""LF"",(LLint)HTS_STAT.stat_bytes);
  3679.           fclose(fp);
  3680.         }
  3681.       }
  3682.       stat_fragment=HTS_STAT.stat_bytes;
  3683.       /* Info for wrappers */
  3684.       if ( (opt->debug>0) && (opt->log!=NULL) ) {
  3685.         HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: pause: %s"LF,fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-paused.lock"));
  3686.       }
  3687.       RUN_CALLBACK1(opt, pause, fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-paused.lock"));
  3688.     }
  3689.     //
  3690.   }
  3691.   // end of pause/lock files
  3692.  
  3693.   // changement dans les prΘfΘrences
  3694.   if (opt->state._hts_addurl) {
  3695.     char BIGSTK add_adr[HTS_URLMAXSIZE*2];
  3696.     char BIGSTK add_fil[HTS_URLMAXSIZE*2];
  3697.     while(*opt->state._hts_addurl) {
  3698.       char BIGSTK add_url[HTS_URLMAXSIZE*2];
  3699.       add_adr[0]=add_fil[0]=add_url[0]='\0';
  3700.       if (!link_has_authority(*opt->state._hts_addurl))
  3701.         strcpybuff(add_url,"http://");          // ajouter http://
  3702.       strcatbuff(add_url,*opt->state._hts_addurl);
  3703.       if (ident_url_absolute(add_url,add_adr,add_fil)>=0) {
  3704.         // ----Ajout----
  3705.         // noter NOUVEAU lien
  3706.         char BIGSTK add_sav[HTS_URLMAXSIZE*2];
  3707.         // calculer lien et Θventuellement modifier addresse/fichier
  3708.         if (url_savename(add_adr,add_fil,add_sav,NULL,NULL,NULL,NULL,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,NULL)!=-1) { 
  3709.           if (hash_read(hash,add_sav,"",0,0)<0) {      // n'existe pas dΘja
  3710.             // enregistrer lien (MACRO)
  3711.             liens_record(add_adr,add_fil,add_sav,"","");
  3712.             if (liens[lien_tot]!=NULL) {    // OK, pas d'erreur
  3713.               liens[lien_tot]->testmode=0;          // mode test?
  3714.               liens[lien_tot]->link_import=0;       // mode normal
  3715.               liens[lien_tot]->depth=opt->depth;
  3716.               liens[lien_tot]->pass2=max(0,numero_passe);
  3717.               liens[lien_tot]->retry=opt->retry;
  3718.               liens[lien_tot]->premier=lien_tot;
  3719.               liens[lien_tot]->precedent=lien_tot;
  3720.               lien_tot++;
  3721.               //
  3722.               if ((opt->debug>0) && (opt->log!=NULL)) {
  3723.                 HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"Link added by user: %s%s"LF,add_adr,add_fil); test_flush;
  3724.               }
  3725.               //
  3726.             } else {  // oups erreur, plus de mΘmoire!!
  3727.               printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  3728.               if (opt->log) {
  3729.                 fprintf(opt->log,"Not enough memory, can not re-allocate %d bytes"LF,(int)((add_tab_alloc+1)*sizeof(lien_url)));
  3730.                 test_flush;
  3731.               }
  3732.               //if (opt->getmode & 1) { if (fp) { fclose(fp); fp=NULL; } }
  3733.               XH_uninit;    // dΘsallocation mΘmoire & buffers
  3734.               return 0;
  3735.             }
  3736.           } else {
  3737.             if ( (opt->debug>0) && (opt->log!=NULL) ) {
  3738.               HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Existing link %s%s not added after user request"LF,add_adr,add_fil);
  3739.               test_flush;
  3740.             }
  3741.           }
  3742.  
  3743.         }
  3744.       } else {
  3745.         if (opt->log) {
  3746.           HTS_LOG(opt,LOG_ERROR);
  3747.           fprintf(opt->log,"Error during URL decoding for %s"LF,add_url);
  3748.           test_flush;
  3749.         }
  3750.       }
  3751.       // ----Fin Ajout----
  3752.       opt->state._hts_addurl++;                  // suivante
  3753.     }
  3754.     opt->state._hts_addurl=NULL;           // libΘrer _hts_addurl
  3755.   }
  3756.   // si une pause a ΘtΘ demandΘe
  3757.   if (opt->state._hts_setpause || back_pluggable_sockets_strict(sback, opt) <= 0) {
  3758.     // index du lien actuel
  3759.     int b=back_index(opt,sback,urladr,urlfil,savename);
  3760.     int prev = opt->state._hts_in_html_parsing;
  3761.     if (b<0) b=0;    // forcer pour les stats
  3762.     while(opt->state._hts_setpause || back_pluggable_sockets_strict(sback, opt) <= 0) {    // on fait la pause..
  3763.       opt->state._hts_in_html_parsing = 6;
  3764.       back_wait(sback,opt,cache,HTS_STAT.stat_timestart);
  3765.  
  3766.       // Transfer rate
  3767.       engine_stats();
  3768.  
  3769.       // Refresh various stats
  3770.       HTS_STAT.stat_nsocket=back_nsoc(sback);
  3771.       HTS_STAT.stat_errors=fspc(opt,NULL,"error");
  3772.       HTS_STAT.stat_warnings=fspc(opt,NULL,"warning");
  3773.       HTS_STAT.stat_infos=fspc(opt,NULL,"info");
  3774.       HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr);
  3775.       HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback);
  3776.  
  3777.       if (!RUN_CALLBACK7(opt, loop, sback->lnk, sback->count, b,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)) {
  3778.         if (opt->log) {
  3779.           HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"Exit requested by shell or user"LF);
  3780.           test_flush;
  3781.         }
  3782.         *stre->exit_xh_=1;  // exit requested
  3783.         XH_uninit;
  3784.         return 0;
  3785.       }
  3786.       Sleep(100);  // pause
  3787.     }
  3788.     opt->state._hts_in_html_parsing = prev;
  3789.   }
  3790.  
  3791.   // si le fichier n'est pas en backing, le mettre..
  3792.   if (!back_exist(sback,opt,urladr,urlfil,savename)) {
  3793. #if BDEBUG==1
  3794.     printf("crash backing: %s%s\n",liens[ptr]->adr,liens[ptr]->fil);
  3795. #endif
  3796.     if (back_add(sback,opt,cache,urladr,urlfil,savename,liens[liens[ptr]->precedent]->adr,liens[liens[ptr]->precedent]->fil,liens[ptr]->testmode)==-1) {
  3797.       printf("PANIC! : Crash adding error, unexpected error found.. [%d]\n",__LINE__);
  3798. #if BDEBUG==1
  3799.       printf("error while crash adding\n");
  3800. #endif
  3801.       if (opt->log) {
  3802.         HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Unexpected backing error for %s%s"LF,urladr,urlfil);
  3803.         test_flush;
  3804.       } 
  3805.  
  3806.     }
  3807.   }
  3808.  
  3809. #if BDEBUG==1
  3810.   printf("test number of socks\n");
  3811. #endif
  3812.  
  3813.   // ajouter autant de socket qu'on peut ajouter
  3814.   n=opt->maxsoc-back_nsoc(sback);
  3815. #if BDEBUG==1
  3816.   printf("%d sockets available for backing\n",n);
  3817. #endif
  3818.  
  3819.   if ((n>0) && (!opt->state._hts_setpause)) {   // si sockets libre et pas en pause, ajouter
  3820.     // remplir autant que l'on peut le cache (backing)
  3821.     back_fillmax(sback,opt,cache,liens,ptr,numero_passe,lien_tot);
  3822.   }
  3823.  
  3824.   // index du lien actuel
  3825.   {
  3826.     // ------------------------------------------------------------
  3827.     // attendre que le fichier actuel soit prΩt - BOUCLE D'ATTENTE
  3828.     do {
  3829.  
  3830.       // index du lien actuel
  3831.       b=back_index(opt,sback,urladr,urlfil,savename);
  3832. #if BDEBUG==1
  3833.       printf("back index %d, waiting\n",b);
  3834. #endif
  3835.       // Continue to the loop if link still present
  3836.       if (b<0)
  3837.         break;
  3838.  
  3839.       // Receive data
  3840.       if (back[b].status>0)
  3841.         back_wait(sback,opt,cache,HTS_STAT.stat_timestart);
  3842.  
  3843.       // Continue to the loop if link still present
  3844.       b=back_index(opt,sback,urladr,urlfil,savename);
  3845.       if (b<0)
  3846.         break;
  3847.  
  3848.       // Stop the mirror
  3849.       if (!back_checkmirror(opt)) {
  3850.         *stre->exit_xh_=1;  // exit requested
  3851.         XH_uninit;
  3852.         return 0;
  3853.       }
  3854.  
  3855.       // And fill the backing stack
  3856.       if (back[b].status>0)
  3857.         back_fillmax(sback,opt,cache,liens,ptr,numero_passe,lien_tot);
  3858.  
  3859.       // Continue to the loop if link still present
  3860.       b=back_index(opt,sback,urladr,urlfil,savename);
  3861.       if (b<0)
  3862.         break;
  3863.  
  3864.       // autres occupations de HTTrack: statistiques, boucle d'attente, etc.
  3865.       if ((opt->makestat) || (opt->maketrack)) {
  3866.         TStamp l=time_local();
  3867.         if ((int) (l-makestat_time) >= 60) {   
  3868.           if (makestat_fp != NULL) {
  3869.             fspc(NULL,makestat_fp,"info");
  3870.             fprintf(makestat_fp,"Rate= %d (/"LLintP") \11NewLinks= %d (/%d)"LF,(int) ((HTS_STAT.HTS_TOTAL_RECV-*stre->makestat_total_)/(l-makestat_time)), (LLint)HTS_STAT.HTS_TOTAL_RECV,(int) lien_tot-*stre->makestat_lnk_,(int) lien_tot);
  3871.             fflush(makestat_fp);
  3872.             *stre->makestat_total_=HTS_STAT.HTS_TOTAL_RECV;
  3873.             *stre->makestat_lnk_=lien_tot;
  3874.           }
  3875.           if (stre->maketrack_fp != NULL) {
  3876.             int i;
  3877.             fspc(NULL,stre->maketrack_fp,"info"); fprintf(stre->maketrack_fp,LF);
  3878.             for(i=0;i<back_max;i++) {
  3879.               back_info(sback,i,3,stre->maketrack_fp);
  3880.             }
  3881.             fprintf(stre->maketrack_fp,LF);
  3882.             fflush(stre->maketrack_fp);
  3883.  
  3884.           }
  3885.           makestat_time=l;
  3886.         }
  3887.       }
  3888.  
  3889.       /* cancel links */
  3890.             {
  3891.         int i;
  3892.         char* s;
  3893.         while(( s = hts_cancel_file_pop(opt) ) != NULL) {
  3894.           if (strnotempty(s)) {    // fichier α canceller
  3895.             for(i = 0 ; i < back_max ; i++) {
  3896.               if ((back[i].status > 0)) {
  3897.                 if (strcmp(back[i].url_sav,s) == 0) {  // ok trouvΘ
  3898.                   if (back[i].status != 1000) {
  3899. #if HTS_DEBUG_CLOSESOCK
  3900.                     DEBUG_W("user cancel: deletehttp\n");
  3901. #endif
  3902.                     if (back[i].r.soc!=INVALID_SOCKET)
  3903.                       deletehttp(&back[i].r);
  3904.                     back[i].r.soc=INVALID_SOCKET;
  3905.                     back[i].r.statuscode=STATUSCODE_INVALID;
  3906.                     strcpybuff(back[i].r.msg,"Cancelled by User");
  3907.                     back[i].status=0;  // terminΘ
  3908.                     back_set_finished(sback, i);
  3909.                   } else    // cancel ftp.. flag α 1
  3910.                     back[i].stop_ftp = 1;
  3911.                 }
  3912.               }
  3913.             }
  3914.             s[0]='\0';
  3915.           }
  3916.           freet(s);
  3917.         }
  3918.  
  3919.         // Transfer rate
  3920.         engine_stats();
  3921.  
  3922.         // Refresh various stats
  3923.         HTS_STAT.stat_nsocket=back_nsoc(sback);
  3924.         HTS_STAT.stat_errors=fspc(opt,NULL,"error");
  3925.         HTS_STAT.stat_warnings=fspc(opt,NULL,"warning");
  3926.         HTS_STAT.stat_infos=fspc(opt,NULL,"info");
  3927.         HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr);
  3928.         HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback);
  3929.  
  3930.         if (!RUN_CALLBACK7(opt, loop, sback->lnk, sback->count, b,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)) {
  3931.           if (opt->log) {
  3932.             HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"Exit requested by shell or user"LF);
  3933.             test_flush;
  3934.           } 
  3935.           *stre->exit_xh_=1;  // exit requested
  3936.           XH_uninit;
  3937.           return 0;
  3938.         }
  3939.  
  3940.       }
  3941.  
  3942. #if HTS_POLL
  3943.       if ((opt->shell) || (opt->keyboard) || (opt->verbosedisplay) || (!opt->quiet)) {
  3944.         TStamp tl;
  3945.         *stre->info_shell_=1;
  3946.  
  3947.         /* Toggle with ENTER */
  3948.         if (!opt->quiet) {
  3949.           if (check_stdin()) {
  3950.             char com[256];
  3951.             linput(stdin,com,200);
  3952.             if (opt->verbosedisplay==2)
  3953.               opt->verbosedisplay=1;
  3954.             else
  3955.               opt->verbosedisplay=2;
  3956.             /* Info for wrappers */
  3957.             if ( (opt->debug>0) && (opt->log!=NULL) ) {
  3958.               HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: change-options"LF);
  3959.             }
  3960.             RUN_CALLBACK0(opt, chopt);
  3961.           }
  3962.         }
  3963.  
  3964.         tl=time_local();
  3965.  
  3966.         // gΘnΘrer un message d'infos sur l'Θtat actuel
  3967.         if (opt->shell) {    // si shell
  3968.           if ((tl-*stre->last_info_shell_)>0) {    // toute les 1 sec
  3969.             FILE* fp=stdout;
  3970.             int a=0;
  3971.             *stre->last_info_shell_=tl;
  3972.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-autopsy"))) {  // dΘbuggage: teste si le robot est vivant
  3973.               // (oui je sais un robot vivant.. mais bon.. il a le droit de vivre lui aussi)
  3974.               // (libΘrons les robots esclaves de l'internet!)
  3975.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-autopsy"));
  3976.               fp=fopen(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-isalive"),"wb");
  3977.               a=1;
  3978.             }
  3979.             if ((*stre->info_shell_) || a) {
  3980.               int i,j;
  3981.  
  3982.               fprintf(fp,"TIME %d"LF,(int) (tl-HTS_STAT.stat_timestart));
  3983.               fprintf(fp,"TOTAL %d"LF,(int) HTS_STAT.stat_bytes);
  3984.               fprintf(fp,"RATE %d"LF,(int) (HTS_STAT.HTS_TOTAL_RECV/(tl-HTS_STAT.stat_timestart)));
  3985.               fprintf(fp,"SOCKET %d"LF,back_nsoc(sback));
  3986.               fprintf(fp,"LINK %d"LF,lien_tot);
  3987.               {
  3988.                 LLint mem=0;
  3989.                 for(i=0;i<back_max;i++)
  3990.                   if (back[i].r.adr!=NULL)
  3991.                     mem+=back[i].r.size;
  3992.                 fprintf(fp,"INMEM "LLintP""LF,(LLint)mem);
  3993.               }
  3994.               for(j=0;j<2;j++) {  // passes pour ready et wait
  3995.                 for(i=0;i<back_max;i++) {
  3996.                   back_info(sback,i,j+1,stdout);    // maketrack_fp a la place de stdout ?? // **
  3997.                 }
  3998.               }
  3999.               fprintf(fp,LF);
  4000.               if (a)
  4001.                 fclose(fp);
  4002.               io_flush;
  4003.             }
  4004.           }
  4005.         }  // si shell
  4006.  
  4007.       }  // si shell ou keyboard (option)
  4008.       //
  4009. #endif
  4010.     } while((b>=0) && (back[max(b,0)].status>0));
  4011.  
  4012.  
  4013.     // If link not found on the stack, it's because it has already been downloaded
  4014.     // in background
  4015.     // Then, skip it and go to the next one
  4016.     if (b<0) {
  4017.       if ((opt->debug>1) && (opt->log!=NULL)) {
  4018.         HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link #%d is ready, no more on the stack, skipping: %s%s.."LF,ptr,urladr,urlfil);
  4019.         test_flush;
  4020.       }
  4021.  
  4022.       // prochain lien
  4023.       // ptr++;
  4024.  
  4025.       return 2; // goto jump_if_done;
  4026.  
  4027.     }
  4028. #if 0
  4029.     /* FIXME - finalized HAS NO MORE THIS MEANING */
  4030.     /* link put in cache by the backing system for memory spare - reclaim */
  4031.     else if (back[b].finalized) {
  4032.       assertf(back[b].r.adr == NULL);
  4033.       /* read file in cache */
  4034.       back[b].r = cache_read_ro(opt,cache,back[b].url_adr,back[b].url_fil,back[b].url_sav, back[b].location_buffer);
  4035.       /* ensure correct location buffer set */
  4036.       back[b].r.location=back[b].location_buffer;
  4037.       if (back[b].r.statuscode == STATUSCODE_INVALID) {
  4038.         if (opt->log) {
  4039.           HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Unexpected error: %s%s not found anymore in cache"LF,back[b].url_adr,back[b].url_fil);
  4040.           test_flush;
  4041.         }
  4042.       } else {
  4043.         if ( (opt->debug>1) && (opt->log!=NULL) ) {
  4044.           HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"reclaim file %s%s (%d)"LF,back[b].url_adr,back[b].url_fil,back[b].r.statuscode); test_flush;
  4045.         }
  4046.       }
  4047.     }
  4048. #endif
  4049.  
  4050.     if (!opt->verbosedisplay) {
  4051.       if (!opt->quiet) {
  4052.         static int roll=0;  /* static: ok */
  4053.         roll=(roll+1)%4;
  4054.         printf("%c\x0d",("/-\\|")[roll]);
  4055.         fflush(stdout);
  4056.       }
  4057.     } else if (opt->verbosedisplay==1) {
  4058.       if (b >= 0) {
  4059.         if (back[b].r.statuscode==HTTP_OK)
  4060.           printf("%d/%d: %s%s ("LLintP" bytes) - OK\33[K\r",ptr,lien_tot,back[b].url_adr,back[b].url_fil,(LLint)back[b].r.size);
  4061.         else
  4062.           printf("%d/%d: %s%s ("LLintP" bytes) - %d\33[K\r",ptr,lien_tot,back[b].url_adr,back[b].url_fil,(LLint)back[b].r.size,back[b].r.statuscode);
  4063.       } else {
  4064.         HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Link disappeared");
  4065.       }
  4066.       fflush(stdout);
  4067.     }
  4068.     //}
  4069.  
  4070.         // ------------------------------------------------------------
  4071.     // VΘrificateur d'intΘgritΘ
  4072. #if DEBUG_CHECKINT
  4073.     _CHECKINT(&back[b],"Retour de back_wait, aprΦs le while")
  4074.     {
  4075.       int i;
  4076.       for(i=0;i<back_max;i++) {
  4077.         char si[256];
  4078.         sprintf(si,"Test global aprΦs back_wait, index %d",i);
  4079.         _CHECKINT(&back[i],si)
  4080.       }
  4081.     }
  4082. #endif
  4083.  
  4084.     // copier structure rΘponse htsblk
  4085.     if (b >= 0) {
  4086.       memcpy(r, &(back[b].r), sizeof(htsblk));
  4087.       r->location=stre->loc_;    // ne PAS copier location!! adresse, pas de buffer
  4088.       if (back[b].r.location) 
  4089.         strcpybuff(r->location,back[b].r.location);
  4090.       back[b].r.adr=NULL;    // ne pas faire de desalloc ensuite
  4091.  
  4092.       // libΘrer emplacement backing
  4093.       back_maydelete(opt,cache,sback,b);
  4094.     }
  4095.  
  4096.     // progression
  4097. #if 0
  4098.     if (opt->aff_progress) {
  4099.       TStamp tl=time_local();
  4100.       if ((tl-HTS_STAT.stat_timestart)>0) {
  4101.         char s[32];
  4102.         int i=0;
  4103.         lastime=tl;
  4104.         _CLRSCR; _GOTOXY("1","1");
  4105.         printf("Rate=%d B/sec\n",(int) (HTS_STAT.HTS_TOTAL_RECV/(tl-HTS_STAT.stat_timestart)));
  4106.         while(i<minimum(back_max,99)) {  // **
  4107.           if (back[i].status>=0) {  // loading..
  4108.             s[0]='\0';
  4109.             if (strlen(back[i].url_fil)>16)
  4110.               strcatbuff(s,back[i].url_fil+strlen(back[i].url_fil)-16);       
  4111.             else
  4112.               strncatbuff(s,back[i].url_fil,16);
  4113.             printf("%s : ",s);
  4114.  
  4115.             printf("[");
  4116.             if (back[i].r.totalsize>0) {
  4117.               int p;
  4118.               int j;
  4119.               p=(int)((back[i].r.size*10)/back[i].r.totalsize);
  4120.               p=minimum(10,p);
  4121.               for(j=0;j<p;j++) printf("*");
  4122.               for(j=0;j<(10-p);j++) printf("-");
  4123.             } else { 
  4124.               printf(LLintP,(LLint)back[i].r.size);                      
  4125.             }
  4126.             printf("]");
  4127.  
  4128.             //} else if (back[i].status==STATUS_READY) {
  4129.             //  strcpybuff(s,"ENDED");
  4130.           } 
  4131.           printf("\n");
  4132.           i++;
  4133.         }
  4134.         io_flush;
  4135.       }
  4136.     }
  4137. #endif
  4138.  
  4139.     // dΘbug graphique
  4140. #if BDEBUG==2
  4141.     {
  4142.       char s[12];
  4143.       int i=0;
  4144.       _GOTOXY(1,1);
  4145.       printf("Rate=%d B/sec\n",(int) (HTS_STAT.HTS_TOTAL_RECV/(time_local()-HTS_STAT.stat_timestart)));
  4146.       while(i<minimum(back_max,160)) {
  4147.         if (back[i].status>0) {
  4148.           sprintf(s,"%d",back[i].r.size);
  4149.         } else if (back[i].status==STATUS_READY) {
  4150.           strcpybuff(s,"ENDED");
  4151.         } else 
  4152.           strcpybuff(s,"   -   ");
  4153.         while(strlen(s)<8) strcatbuff(s," ");
  4154.         printf("%s",s); io_flush;
  4155.         i++;
  4156.       }
  4157.     }
  4158. #endif
  4159.  
  4160.  
  4161. #if BDEBUG==1
  4162.     printf("statuscode=%d with %s / msg=%s\n",r->statuscode,r->contenttype,r->msg);
  4163. #endif
  4164.  
  4165.   }
  4166.   /*else {
  4167.   #if BDEBUG==1
  4168.   printf("back index error\n");
  4169.   #endif
  4170.   }
  4171.   */
  4172.  
  4173.   ENGINE_SAVE_CONTEXT();
  4174.   return 0;
  4175. }
  4176.  
  4177. /* Wait for delayed types */
  4178. int hts_wait_delayed(htsmoduleStruct* str, 
  4179.                      char* adr, char* fil, char* save, 
  4180.                      char* parent_adr, char* parent_fil,
  4181.                      char* former_adr, char* former_fil, 
  4182.                      int* forbidden_url) {
  4183.   ENGINE_LOAD_CONTEXT_BASE();
  4184.   hash_struct* const hash = hashptr;
  4185.  
  4186.   int r_sv=0;
  4187.  
  4188.   // resolve unresolved type
  4189.   if (opt->savename_delayed != 0
  4190.     && *forbidden_url == 0 
  4191.     && IS_DELAYED_EXT(save) 
  4192.     && !opt->state.stop
  4193.     )
  4194.   {
  4195.     int loops;
  4196.     int continue_loop;
  4197.     if ((opt->debug>1) && (opt->log!=NULL)) {
  4198.       HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Waiting for type to be known: %s%s"LF, adr, fil);
  4199.       test_flush;
  4200.     }
  4201.  
  4202.     /* Follow while type is unknown and redirects occurs */
  4203.     for( loops = 0, continue_loop = 1 ; IS_DELAYED_EXT(save) && continue_loop && loops < 7 ; loops++  ) {
  4204.       continue_loop = 0;
  4205.  
  4206.       /*
  4207.       Wait for an available slot 
  4208.       */
  4209.       WAIT_FOR_AVAILABLE_SOCKET();
  4210.  
  4211.       /* We can lookup directly in the cache to speedup this mess */
  4212.       if (opt->delayed_cached) {
  4213.                 lien_back back;
  4214.                 memset(&back, 0, sizeof(back));
  4215.                 back.r = cache_read(opt, cache, adr, fil, NULL, NULL);              // test uniquement
  4216.         if (back.r.statuscode == HTTP_OK && strnotempty(back.r.contenttype)) {      // cache found, and aswer is 'OK'
  4217.           if ((opt->debug>1) && (opt->log!=NULL)) {
  4218.             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Direct type lookup in cache (-%%D1): %s"LF, back.r.contenttype);
  4219.             test_flush;
  4220.           }
  4221.  
  4222.           /* Recompute filename with MIME type */
  4223.           save[0] = '\0';
  4224.           r_sv=url_savename(adr,fil,save,former_adr,former_fil,liens[ptr]->adr,liens[ptr]->fil,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,&back);
  4225.  
  4226.           /* Recompute authorization with MIME type */
  4227.           {
  4228.             int new_forbidden_url = hts_acceptmime(opt, ptr, lien_tot, liens, adr,fil, back.r.contenttype);
  4229.             if (new_forbidden_url != -1) {
  4230.               if ((opt->debug>1) && (opt->log!=NULL)) {
  4231.                 HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"result for wizard mime test: %d"LF,new_forbidden_url);
  4232.                 test_flush;
  4233.               }
  4234.               if (new_forbidden_url == 1) {
  4235.                 *forbidden_url = new_forbidden_url;
  4236.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  4237.                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link forbidden because of MIME types restrictions: %s%s"LF, adr, fil);
  4238.                   test_flush;
  4239.                 }
  4240.                 break;        // exit loop
  4241.               }
  4242.             }
  4243.           }
  4244.  
  4245.           /* And exit loop */
  4246.           break;
  4247.         }
  4248.       }
  4249.  
  4250.       /* Check if the file was recorded already (necessary for redirects) */
  4251.       if (hash_read(hash,save,"",0,opt->urlhack) >= 0) {
  4252.         if (loops == 0) {   /* Should not happend */
  4253.           if ( opt->log!=NULL ) {
  4254.             HTS_LOG(opt,LOG_ERROR); fprintf(opt->log, "Duplicate entry in hts_wait_delayed() cancelled: %s%s -> %s"LF,adr,fil,save);
  4255.             test_flush;
  4256.           }
  4257.         }
  4258.         /* Exit loop (we're done) */
  4259.         continue_loop = 0;
  4260.         break ;
  4261.       }
  4262.  
  4263.       /* Add in backing (back_index() will respond correctly) */
  4264.       if (back_add_if_not_exists(sback,opt,cache,adr,fil,save,parent_adr,parent_fil,0) != -1) {
  4265.         int b;
  4266.         b=back_index(opt,sback,adr,fil,save); 
  4267.         if (b<0) {
  4268.           printf("PANIC! : Crash adding error, unexpected error found.. [%d]\n",__LINE__);
  4269.           XH_uninit;    // dΘsallocation mΘmoire & buffers
  4270.           return -1;
  4271.         }
  4272.  
  4273.         /* Cache read failed because file does not exists (bad delayed name!)
  4274.         Just re-add with the correct name, as we know the MIME now!
  4275.         */
  4276.         if (back[b].r.statuscode == STATUSCODE_INVALID && back[b].r.adr == NULL) {
  4277.                     lien_back delayed_back;
  4278.           //char BIGSTK delayed_ctype[128];
  4279.           // delayed_ctype[0] = '\0';
  4280.           // strncatbuff(delayed_ctype, back[b].r.contenttype, sizeof(delayed_ctype) - 1);    // copier content-type
  4281.                     back_copy_static(&back[b], &delayed_back);
  4282.  
  4283.           /* Delete entry */
  4284.           back_delete(opt,cache,sback,b);       // cancel
  4285.           b = -1;
  4286.  
  4287.           /* Recompute filename with MIME type */
  4288.           save[0] = '\0';
  4289.           r_sv=url_savename(adr,fil,save,former_adr,former_fil,liens[ptr]->adr,liens[ptr]->fil,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,&delayed_back);
  4290.  
  4291.           /* Recompute authorization with MIME type */
  4292.           {
  4293.             int new_forbidden_url = hts_acceptmime(opt, ptr, lien_tot, liens, adr,fil, delayed_back.r.contenttype);
  4294.             if (new_forbidden_url != -1) {
  4295.               if ((opt->debug>1) && (opt->log!=NULL)) {
  4296.                 HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"result for wizard mime test: %d"LF,*forbidden_url);
  4297.                 test_flush;
  4298.               }
  4299.               if (new_forbidden_url == 1) {
  4300.                 *forbidden_url = new_forbidden_url;
  4301.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  4302.                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link forbidden because of MIME types restrictions: %s%s"LF, adr, fil);
  4303.                   test_flush;
  4304.                 }
  4305.                 break;        // exit loop
  4306.               }
  4307.             }
  4308.           }
  4309.  
  4310.           /* Re-Add wiht correct type */
  4311.           if (back_add_if_not_exists(sback,opt,cache,adr,fil,save,parent_adr,parent_fil,0) != -1) {
  4312.             b=back_index(opt,sback,adr,fil,save); 
  4313.           }
  4314.           if (b<0) {
  4315.             printf("PANIC! : Crash adding error, unexpected error found.. [%d]\n",__LINE__);
  4316.             XH_uninit;    // dΘsallocation mΘmoire & buffers
  4317.             return -1;
  4318.           }
  4319.           if ((opt->debug>1) && (opt->log!=NULL)) {
  4320.             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Type immediately loaded from cache: %s"LF, delayed_back.r.contenttype);
  4321.             test_flush;
  4322.           }
  4323.         }
  4324.  
  4325.         /* Wait for headers to be received */
  4326.         if (b >= 0) {
  4327.           back_set_locked(sback, b);    // Locked entry
  4328.         }
  4329.         do {
  4330.           if (b < 0)
  4331.             break;
  4332.  
  4333.           // temps α attendre, et remplir autant que l'on peut le cache (backing)
  4334.           if (back[b].status>0) {
  4335.             back_wait(sback,opt,cache,0);
  4336.           }
  4337.           if (ptr>=0) {
  4338.             back_fillmax(sback,opt,cache,liens,ptr,numero_passe,lien_tot);
  4339.           }
  4340.  
  4341.           // on est obligΘ d'appeler le shell pour le refresh..
  4342.           {
  4343.  
  4344.             // Transfer rate
  4345.             engine_stats();
  4346.  
  4347.             // Refresh various stats
  4348.             HTS_STAT.stat_nsocket=back_nsoc(sback);
  4349.             HTS_STAT.stat_errors=fspc(opt,NULL,"error");
  4350.             HTS_STAT.stat_warnings=fspc(opt,NULL,"warning");
  4351.             HTS_STAT.stat_infos=fspc(opt,NULL,"info");
  4352.             HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr);
  4353.             HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback);
  4354.  
  4355.             if (!RUN_CALLBACK7(opt, loop, sback->lnk, sback->count, b,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)) {
  4356.               return -1;
  4357.             } else if (opt->state._hts_cancel || !back_checkmirror(opt)) {    // cancel 2 ou 1 (cancel parsing)
  4358.               back_delete(opt,cache,sback,b);       // cancel test
  4359.               break;
  4360.             }
  4361.           }
  4362.         } while(
  4363.           /* dns/connect/request */ 
  4364.                     ( back[b].status >= 99 && back[b].status <= 101 )
  4365.           ||
  4366.           /* For redirects, wait for request to be terminated */
  4367.           ( HTTP_IS_REDIRECT(back[b].r.statuscode) && back[b].status > 0 )
  4368.           ||
  4369.           /* Same for errors */
  4370.           ( HTTP_IS_ERROR(back[b].r.statuscode) && back[b].status > 0 )
  4371.           );
  4372.         if (b >= 0) {
  4373.           back_set_unlocked(sback, b);    // Unlocked entry
  4374.         }
  4375.         /* ready (chunked) or ready (regular download) or ready (completed) */
  4376.  
  4377.         // Note: filename NOT in hashtable yet - liens_record will do it, with the correct ext!
  4378.         if (b >= 0) {
  4379.                     lien_back delayed_back;
  4380.           //char BIGSTK delayed_ctype[128];
  4381.           //delayed_ctype[0] = '\0';
  4382.           //strncatbuff(delayed_ctype, back[b].r.contenttype, sizeof(delayed_ctype) - 1);    // copier content-type
  4383.                     back_copy_static(&back[b], &delayed_back);
  4384.  
  4385.           /* Error */
  4386.           if (HTTP_IS_ERROR(back[b].r.statuscode))
  4387.           {
  4388.             /* 'no error page' selected or file discarded by size rules! */
  4389.             if (!opt->errpage || ( back[b].r.statuscode == STATUSCODE_TOO_BIG ) ) {
  4390.               /* Note: the cache 'cached_tests' system will remember this error, and we'll only issue ONE request */
  4391.               *forbidden_url = 1;          /* Forbidden! */
  4392.               if (opt->log != NULL) {
  4393.                 if (back[b].r.statuscode == STATUSCODE_TOO_BIG) {
  4394.                   HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"link not taken because of its size (%d bytes) at %s%s"LF,(int)back[b].r.totalsize,adr,fil);
  4395.                 } else {
  4396.                   HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"link not taken because of error (%d '%s') at %s%s"LF,back[b].r.statuscode,back[b].r.msg,adr,fil);
  4397.                 }
  4398.                 test_flush;
  4399.               }
  4400.               break;
  4401.             }
  4402.           }
  4403.           /* Moved! */
  4404.           else if (HTTP_IS_REDIRECT(back[b].r.statuscode))
  4405.           {
  4406.             char BIGSTK mov_url[HTS_URLMAXSIZE*2];
  4407.             mov_url[0] = '\0';
  4408.             strcpybuff(mov_url, back[b].r.location);    // copier URL
  4409.  
  4410.             /* Remove (temporarily created) file if it was created */
  4411.             unlink(fconv(OPT_GET_BUFF(opt),back[b].url_sav));
  4412.  
  4413.             /* Remove slot! */
  4414.             if (back[b].status == STATUS_READY) {
  4415.               back_maydelete(opt, cache, sback, b);
  4416.             } else {    /* should not happend */
  4417.               back_delete(opt, cache, sback, b);
  4418.             }
  4419.             b = -1;
  4420.  
  4421.             /* Handle redirect */
  4422.             if ((int) strnotempty(mov_url)) {    // location existe!
  4423.               char BIGSTK mov_adr[HTS_URLMAXSIZE*2],mov_fil[HTS_URLMAXSIZE*2];
  4424.               mov_adr[0]=mov_fil[0]='\0';
  4425.               //
  4426.               if (ident_url_relatif(mov_url,adr,fil,mov_adr,mov_fil)>=0) {                        
  4427.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  4428.                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Redirect while resolving type: %s%s -> %s%s"LF, adr, fil, mov_adr, mov_fil);
  4429.                   test_flush;
  4430.                 }
  4431.                 // si non bouclage sur soi mΩme, ou si test avec GET non testΘ
  4432.                 if (strcmp(mov_adr,adr) != 0 || strcmp(mov_fil,fil) != 0) {
  4433.  
  4434.                   // recopier former_adr/fil?
  4435.                   if ((former_adr) && (former_fil)) {
  4436.                     if (strnotempty(former_adr)==0) {    // Pas dΘja notΘ
  4437.                       strcpybuff(former_adr,adr);
  4438.                       strcpybuff(former_fil,fil);
  4439.                     }
  4440.                   }
  4441.  
  4442.                   // check explicit forbidden - don't follow 3xx in this case
  4443.                   {
  4444.                     int set_prio_to=0;
  4445.                     if (hts_acceptlink(opt,ptr,lien_tot,liens,
  4446.                       mov_adr,mov_fil,
  4447.                       NULL, NULL,
  4448.                       &set_prio_to,
  4449.                       NULL) == 1) 
  4450.                     {  /* forbidden */
  4451.                       /* Note: the cache 'cached_tests' system will remember this error, and we'll only issue ONE request */
  4452.                       *forbidden_url = 1;          /* Forbidden! */
  4453.                       if ((opt->debug>1) && (opt->log!=NULL)) {
  4454.                         HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link forbidden because of redirect beyond the mirror scope at %s%s -> %s%s"LF,adr,fil,mov_adr,mov_fil);
  4455.                         test_flush;
  4456.                       }
  4457.                       strcpybuff(adr,mov_adr);
  4458.                       strcpybuff(fil,mov_fil);
  4459.                       mov_url[0]='\0';
  4460.                       break;
  4461.                     }
  4462.                   }
  4463.  
  4464.                   // ftp: stop!
  4465.                   if (strfield(mov_url,"ftp://")
  4466. #if HTS_USEMMS
  4467.                                         || strfield(mov_url,"mms://")
  4468. #endif
  4469.                                         ) {
  4470.                     strcpybuff(adr,mov_adr);
  4471.                     strcpybuff(fil,mov_fil);
  4472.                     break;
  4473.                   }
  4474.  
  4475.                   /* ok, continue */
  4476.                   strcpybuff(adr,mov_adr);
  4477.                   strcpybuff(fil,mov_fil);
  4478.                   continue_loop = 1;
  4479.  
  4480.                   /* Recompute filename for hash lookup */
  4481.                   save[0] = '\0';
  4482.                   r_sv=url_savename(adr,fil,save,former_adr,former_fil,liens[ptr]->adr,liens[ptr]->fil,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,&delayed_back);
  4483.                 } else {
  4484.                   if ( opt->log!=NULL ) {
  4485.                     HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Unable to test %s%s (loop to same filename)"LF,adr,fil);
  4486.                     test_flush;
  4487.                   }
  4488.                 }  // loop to same location
  4489.               }  // ident_url_relatif()
  4490.             }  // location
  4491.           }  // redirect
  4492.           if ((opt->debug>1) && (opt->log!=NULL)) {
  4493.                         HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Final type for %s%s: '%s'"LF, adr, fil, delayed_back.r.contenttype);
  4494.             test_flush;
  4495.           }
  4496.  
  4497.                     /* If we are done, do additional checks with final type and authorizations */
  4498.                     if (!continue_loop) {
  4499.                         /* Recompute filename with MIME type */
  4500.                         save[0] = '\0';
  4501.                         r_sv=url_savename(adr,fil,save,former_adr,former_fil,liens[ptr]->adr,liens[ptr]->fil,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,&delayed_back);
  4502.  
  4503.                         /* Recompute authorization with MIME type */
  4504.                         {
  4505.                             int new_forbidden_url = hts_acceptmime(opt, ptr, lien_tot, liens, adr,fil, delayed_back.r.contenttype);
  4506.                             if (new_forbidden_url != -1) {
  4507.                                 if ((opt->debug>1) && (opt->log!=NULL)) {
  4508.                                     HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"result for wizard mime test: %d"LF,*forbidden_url);
  4509.                                     test_flush;
  4510.                                 }
  4511.                                 if (new_forbidden_url == 1) {
  4512.                                     *forbidden_url = new_forbidden_url;
  4513.                                     if ((opt->debug>1) && (opt->log!=NULL)) {
  4514.                                         HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link forbidden because of MIME types restrictions: %s%s"LF, adr, fil);
  4515.                                         test_flush;
  4516.                                     }
  4517.                                     break;        // exit loop
  4518.                                 }
  4519.                             }
  4520.                         }
  4521.                     }
  4522.  
  4523.           /* Still have a back reference */
  4524.           if (b >= 0) {
  4525.             /* Finalize now as we have the type */
  4526.                         if (back[b].status == STATUS_READY) {
  4527.                             if (!back[b].finalized) {
  4528.                                 back_finalize(opt,cache,sback,b);
  4529.                             }
  4530.                         }
  4531.             /* Patch destination filename for direct-to-disk mode */
  4532.             strcpybuff(back[b].url_sav, save);
  4533.           }
  4534.  
  4535.         }  // b >= 0
  4536.       } else {
  4537.         printf("PANIC! : Crash adding error, unexpected error found.. [%d]\n",__LINE__);
  4538.         XH_uninit;    // dΘsallocation mΘmoire & buffers
  4539.         return -1;
  4540.       }
  4541.  
  4542.     } // while(IS_DELAYED_EXT(save))
  4543.  
  4544.     // error
  4545.     if (*forbidden_url != 1
  4546.       && IS_DELAYED_EXT(save)) {
  4547.       *forbidden_url = 1;
  4548.       if (opt->log!=NULL) {
  4549.         HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"link is probably looping, type unknown, aborting: %s%s"LF, adr, fil);
  4550.         test_flush;
  4551.       }
  4552.     }
  4553.  
  4554.   }  // delayed type check ?
  4555.  
  4556.   ENGINE_SAVE_CONTEXT_BASE();
  4557.  
  4558.   return 0;
  4559. }
  4560.  
  4561.